PHP和MySQL开发的8个技巧


LAMP 架构的网站,我以前注重的多是安装/配置方面的,讲述开发的相对较少,因为自己从事开发也少。本文的原文当然也来自:

Published on The O'Reilly Network (http://www.oreillynet.com/)
http://www.oreillynet.com/pub/a/onlamp/2002/04/04/webdb.html

看了以后,颇有启发,以前开发中遇到的一些问题,迎刃而解。所以翻译出来和大家共享。

1. PHP 中数组的使用
在操作数据库时,使用关联数组(associatively-indexed arrays)十分有帮助,下面我们看一个基本的数字格式的数组遍历:

1   
2$temp[0] = "richmond";   
3$temp[1] = "tigers";   
4$temp[2] = "premiers"; 
5
6for($x=0;$x

<count($temp);$x++) "="" "$element="" ";="" "premiers");="" "tigers",="" $element)="" $temp='array("club"' $temp[$x];="" ($temp="" =="" ```="" as="" echo="" foreach="" {="" }="" 然而另外一种更加节省代码的方式是:="" 还能输出文字下标:=""> "richmond",
"nickname" =>"tigers",
"aim" => "premiers");

foreach ($temp as $key => $value)
echo "$key : $value ";

1PHP 手册中描述了大约 50 个用于处理数组的函数。 
2
32\. 在 PHP 字符串中加入变量 
4
5这个很简单的: 

$temp = "hello"
echo "$temp world";

1
2但是需要说明的是,尽管下面的例子没有错误:   

$temp = array("one" => 1, "two" => 2);
// 输出:: The first element is 1
echo "The first element is $temp[one].";

1
2但是如果后面那个 echo 语句没有双引号引起来的话,就要报错,因此建议使用花括号: 

$temp = array("one" => 1, "two" => 2);
echo "The first element is {$temp["one"]}.";

1
2
33\. 采用关联数组存取查询结果   
4看下面的例子: 

$connection = mysql_connect("localhost", "albert", "shhh");
mysql_select_db("winestore", $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";
}

 1
 2函数 mysql_fetch_array() 把查询结果的一行放入数组,可以同时用两种方式引用,例如 cust_id 可以同时用下面两种方式:$row["cust_id"] 或者$row[0] 。显然,前者的可读性要比后者好多了。 
 3
 4在多表连查中,如果两个列名字一样,最好用别名分开: 
 5
 6SELECT winery.name AS wname,   
 7region.name AS rname,   
 8FROM winery, region   
 9WHERE winery.region_id = region.region_id;   
10
11
12列名的引用为:$row["wname"] 和 $row["rname"]。   
13
14
15在指定表名和列名的情况下,只引用列名: 
16
17SELECT winery.region_id   
18FROM winery 
19
20列名的引用为: $row["region_id"]。 
21
22聚集函数的引用就是引用名: 
23
24SELECT count(*)   
25FROM customer; 
26
27列名的引用为: $row["count(*)"]。 
28
294\. 注意常见的 PHP bug 
30
31常见的 PHP 纠错问题是: 
32
33No page rendered by the Web browser when much more is expected   
34A pop-up dialog stating that the "Document Contains No Data"   
35A partial page when more is expected 
36
37出现这些情况的大多数原因并不在于脚本的逻辑,而是 HTML 中存在的 bug 或者脚本生成的 HTML 的 bug 。例如缺少类似 , ,  之类的关闭 Tag,页面就不能刷新。解决这个问题的办法就是,查看 HTML 的源代码。 
38
39对于复杂的,不能查到原因的页面,可以通过 W3C 的页面校验程序 http://validator.w3.org/ 来分析。 
40
41如果没有定义变量,或者变量定义错误也会让程序变得古怪。例如下面的死循环: 

for($counter=0; $counter<10; $Counter++)
myFunction();

1
2变量 $Counter 在增加,而 $counter 永远小于 10。这类错误一般都能通过设置较高的错误报告级别来找到: 

error_reporting(E_ALL);

for($counter=0; $counter<10; $Counter++)
myFunction();

 1
 25\. 采用 header() 函数处理单部件查询 
 3
 4在很多 Web 数据库应用中,一些功能往往让用户点击一个连接后,继续停留在当前页面,这样的工作我叫它“单部件查询”。 
 5
 6下面是一个叫做 calling.php 的脚本: 
 7
 8&lt;!DOCTYPE HTML PUBLIC   
 9"-//W3C//DTD HTML 4.0 Transitional//EN"   
10"http://www.w3.org/TR/html4/loose.dtd" &gt;
11
12&lt;html&gt;
13&lt;head&gt;
14&lt;title&gt;Calling page example&lt;/title&gt;
15&lt;/head&gt;
16&lt;body&gt;
17&lt;a href="action.php"&gt;Click here!&lt;/a&gt;
18&lt;/body&gt;
19&lt;/html&gt;
20
21当用户点击上面的连接时,就去调用 action.php。下面是 action.php 的源码: 

// 数据库功能

// 重定向
header("Location: $HTTP_REFERER");
exit;

 1
 2这里有两个常见的错误需要提醒一下:   
 3调用 header() 函数后要包含一个 exit 语句让脚本停止,否则后续的脚本可能会在头发送前输出。   
 4
 5
 6header() 函数常见的一个错误是: 
 7
 8Warning: Cannot add header information - headers already sent... 
 9
10header() 函数只能在 HTML 输出之前被调用,因此你需要检查 php 前面可能存在的空行,空格等等。 
11
126\. reload 的问题及其解决   
13我以前在写 PHP 程序时,经常碰到页面刷新时,数据库多处理一次的情况。   
14我们来看 addcust.php: 

$query = "INSERT INTO customer
SET surname = $surname,
firstname = $firstname";
$connection = mysql_connect("localhost", "fred", "shhh");
mysql_select_db("winestore", $connection);
$result = mysql_query($query, $connection);

 1&lt;!DOCTYPE HTML PUBLIC   
 2"-//W3C//DTD HTML 4.0 Transitional//EN"   
 3"http://www.w3.org/TR/html4/loose.dtd" &gt;
 4
 5&lt;html&gt;
 6&lt;head&gt;
 7&lt;title&gt;Customer insert&lt;/title&gt;
 8&lt;/head&gt;
 9&lt;body&gt;   
10I've inserted the customer for you.   
11&lt;/body&gt;
12&lt;/html&gt;   
13?&amp;gt;   
14假设我们用下面的连接使用这个程序: 
15
16http://www.freelamp.com/addcust.php?surname=Smith&amp;amp;firstname=Fred 
17
18如果这个请求只提交一次,OK ,不会有问题,但是如果多次刷新,你就会有多条记录插入。   
19这个问题可以通过 header() 函数解决:下面是新版本的 addcust.php: 

$query = "INSERT INTO customer
SET surname = $surname,
firstname = $firstname";
$connection = mysql_connect("localhost", "fred", "shhh");
mysql_select_db("winestore", $connection);
$result = mysql_query($query, $connection);
header("Location: cust_receipt.php");

 1这个脚本把浏览器重定向到一个新的页面:cust_receipt.php: 
 2
 3&lt;!DOCTYPE HTML PUBLIC   
 4"-//W3C//DTD HTML 4.0 Transitional//EN"   
 5"http://www.w3.org/TR/html4/loose.dtd" &gt;
 6
 7&lt;html&gt;
 8&lt;head&gt;
 9&lt;title&gt;Customer insert&lt;/title&gt;
10&lt;/head&gt;
11&lt;body&gt;   
12I've inserted the customer for you.   
13&lt;/body&gt;
14&lt;/html&gt;   
15这样,原来的页面继续刷新也没有副作用了。 
16
177\. 巧用锁机制来提高应用性能   
18如果我们要紧急运行一个报表,那么,我们可以对表加写锁,防治别人读写,来提高对这个表的处理速度。 
19
208\. 用 mysql_unbuffered_query() 开发快速的脚本   
21这个函数能用来替换 mysql_query() 函数,主要的区别就是 mysql_unbuffered_query() 执行完查询后马上返回,不需要等待或者对数据库加锁。 
22
23但是返回的行数不能用mysql_num_rows() 函数来检查,因为输出的结果集大小未知。&lt;/count($temp);$x++)&gt;
Published At
Categories with 数据库类
Tagged with
comments powered by Disqus