脚本宝典收集整理的这篇文章主要介绍了PHP 和 MySQL 开发的 8 个技巧,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
1.&nbs
p;PHP 中数组的使用
在操作
数据库时,使用关联数组(associatively
-indexed arrays)十分有帮助,下面我们看
一个基本的数字格式的
数组遍历:
<?
PHP $temp[0] = "richmond";
$temp[1] = "tigers";
$temp[2] = "
PRemiers";
for($x=0;$x<count($temp);$x++)
{
echo $temp[$x];
echo " ";
}
?>
然而另外一种更加
节省代码的方式是:
<?
PHP $temp = array("richmond", "tigers", "premiers");
for
each ($temp as $element)
echo "$element ";
?>
foreach 还能
输出文字下标:
<?
PHP $temp = array("cl
ub" => "richmond",
"nickn
ame" =>"tigers",
"
aim" => "premiers");
foreach ($temp as $key => $value)
echo "$key : $value ";
?>
PHP 手册中描述了大约 50 个用于处理数组的
函数。
2. 在
PHP 字符串中加入变量
这个很
简单的:
<?
PHP $temp = "hello"
echo "$temp world";
?>
但是需要说明的是,尽管下面的例子没有
错误:
<?
PHP $temp = array("one" => 1, "two" => 2);
//
输出:: The
First element is 1
echo "The first element is $temp[one].";
?>
但是如果后面那个 echo 语句没有双引号引起来的话,就要报错,因此建议使用花括号:
<?
PHP $temp = array("one" => 1, "two" => 2);
echo "The first element is {$temp["one"]}.";
?>
3. 采用关联数组
存取查询结果
看下面的例子:
<?
PHP $connection =
MysqL_connect("localhost", "albert", "shhh");
MysqL_select_db("@R_628_2
406@
Store", $connection);
$result =
MysqL_query("SELECT cust_id, surname,
firstname
From customer", $connection);
while ($row =
MysqL_fetch_array($result))
{
echo "ID:\t{$row["cust_id"]}\n";
echo "Surname\t{$row["surname"]}\n";
echo "First name:\t{$row["firstname"]}\n\n";
}
?>
函数 MysqL_fetch_array() 把
查询结果的一行放入数组,可以同时用两种方式引用,例如 cust_id 可以同时用下面两种方式:$row["cust_id"]
或者$row[0] 。显然,前者的
可读性要比后者好多了。
在多表连查中,如果两个列名字一样,最好用别名分开:
SELECT winery.name AS wname,
region.name AS rname,
F
rom winery, region
WHERE winery.region_id = region.region_id;
列名的引用为:$row["wname"] 和 $row["rname"]。
在指定表名和列名的情况下,只引用列名:
SELECT winery.region_id
FROM winery
列名的引用为: $row["region_id"]。
聚集
函数的引用就是引用名:
SELECT count(*)
FROM customer;
列名的引用为: $row["count(*)"]。
4. 注意常见的
PHP bug
常见的
PHP 纠错问题是:
No page rende
red by the Web
browser when 
;much more is e
xpected
A pop-up dia
LOG stating t
hat the "Document Contains No Data"
A partial page when more is ex
PEcted
出现这些情况的大多数
原因并不在于脚本的逻辑,而是 HT
ML 中存在的 bug 或者脚本
生成的 HTML 的 bug 。例如缺少类似 </table>, </form>, </frame> 之类的
关闭 Tag,
页面就不能刷新。
解决这个问题的办法就是,查看 HTML 的
源代码。
对于复杂的,不能查到原因的
页面,可以通过 W3C 的
页面校验程序 http://validator.w3.
org/ 来分析。
如果没有定义变量,或者
变量定义错误也会让程序变得古怪。例如下面的死循环:
<?
PHP for($counter=0; $counter<10; $Counter++)
myFunction();
?>
变量 $Counter 在
增加,而 $counter
永远小于 10。这类
错误一般都能通过设置较高的
错误报告级别来找到:
<?
PHP error_reporting(E_ALL);
for($counter=0; $counter<10; $Counter++)
myFunction();
?>
5. 采用 header()
函数处理单部件
查询 在很多 Web
数据库应用中,一些
功能往往让
用户点击
一个连接后,继续停留在
当前页面,这样的工作我叫它“单部件
查询”。
下面是
一个叫做 calling.
PHP 的脚本:
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Trans
ITional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<title>Calling page example</title>
</head>
<body>
<a
href="action.
PHP">Click here!</a>
</body>
</html>
当
用户点击上面的连接时,就去
调用 action.
PHP。下面是 action.
PHP 的源码:
<?
PHP //
数据库功能 //
重定向 header("Location: $HTTP_REFERER");
exit;
?>
这里有两个常见的
错误需要提醒一下:
调用 header()
函数后要包含
一个 exit 语句让脚本停止,否则后续的脚本可能会在头发送前
输出。
header()
函数常见的
一个错误是:
Warning: Cannot add header information - headers already sent
...
header()
函数只能在 HTML
输出之前被
调用,因此你需要检查
PHP 前面可能存在的空行,空格
等等。
6. reload 的问题及其
解决 我以前在写
PHP 程序时,经常碰到
页面刷新时,
数据库多处理一次的情况。
我们来看 addcust.
PHP:
<?
PHP $query = "INSERT INTO customer
SET surname = $surname,
firstname = $firstname";
$connection =
MysqL_connect("localhost", "fred", $connection);
$result =
MysqL_query($query, $connection);
?>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<title>Customer insert</title>
</head>
<body>
I've inserted the customer for you.
</body>
</html>
?>
假设我们用下面的连接使用这个程序:
http://www.freelamp
.COM/addcust. ... &firstname=Fred
如果这个请求只提交一次,OK ,不会有问题,但是如果多次刷新,你就会有多条记录插入。
这个问题可以通过 header()
函数解决:下面是新版本的 addcust.
PHP:
<?
PHP $query = "INSERT INTO customer
SET surname = $surname, $connection);
header("Location: cust_rec
eipt.
PHP");
?>
这个脚本把浏览器
重定向到
一个新的
页面:cust_receipt.
PHP:
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<title>Customer insert</title>
</head>
<body>
I've inserted the customer for you.
</body>
</html>
这样,
原来的
页面继续刷新也没有副作用了。
7. 巧用锁机制来
提高应用
性能 如果我们要紧急运行
一个报表,那么,我们可以对表加写锁,
防治
别人读写,来提高对这个表的处理速度。
8. 用
MysqL_unbuffered_query() 开发
快速的脚本
这个
函数能用来替换
MysqL_query()
函数,主要的区别就是
MysqL_unbuffered_query() 执行完
查询后马上返回,不需要等待或者对
数据库加锁。
但是返回的行数不能用
MysqL_num_rows()
函数来检查,因为
输出的结果集大小未知。
脚本宝典总结
以上是脚本宝典为你收集整理的PHP 和 MySQL 开发的 8 个技巧全部内容,希望文章能够帮你解决PHP 和 MySQL 开发的 8 个技巧所遇到的问题。
如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。