PHP 和 MySQL 開發的 8 個技巧
2024-07-24 12:56:28
供稿:網友
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)十分有幫助,下面我們看一個基本的數字格式的數組遍歷:
$temp[0] = "richmond";
$temp[1] = "tigers";
$temp[2] = "premiers";
for($x=0;$x
{
echo $temp[$x];
echo " ";
}
?>
然而另外一種更加節省代碼的方式是:
$temp = array("richmond", "tigers", "premiers");
foreach ($temp as $element)
echo "$element ";
?>
foreach 還能輸出文字下標:
$temp = array("club" => "richmond",
"nickname" =>"tigers",
"aim" => "premiers");
foreach ($temp as $key => $value)
echo "$key : $value ";
?>
php 手冊中描述了大約 50 個用于處理數組的函數。
2. 在 php 字符串中加入變量
這個很簡單的:
$temp = "hello"
echo "$temp world";
?>
但是需要說明的是,盡管下面的例子沒有錯誤:
$temp = array("one" => 1, "two" => 2);
// 輸出:: the first element is 1
echo "the first element is $temp[one].";
?>
但是如果后面那個 echo 語句沒有雙引號引起來的話,就要報錯,因此建議使用花括號:
$temp = array("one" => 1, "two" => 2);
echo "the first element is {$temp["one"]}.";
?>
3. 采用關聯數組存取查詢結果
看下面的例子:
$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 "surnamet{$row["surname"]}n";
echo "first name:t{$row["firstname"]}nn";
}
?>
函數 mysql_fetch_array() 把查詢結果的一行放入數組,可以同時用兩種方式引用,例如 cust_id 可以同時用下面兩種方式:$row["cust_id"] 或者$row[0] 。顯然,前者的可讀性要比后者好多了。
在多表連查中,如果兩個列名字一樣,最好用別名分開:
select winery.name as wname,
region.name as rname,
from 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 rendered by the web browser when much more is expected
a pop-up dialog stating that the "document contains no data"
a partial page when more is expected
出現這些情況的大多數原因并不在于腳本的邏輯,而是 html 中存在的 bug 或者腳本生成的 html 的 bug 。例如缺少類似
, , 之類的關閉 tag,頁面就不能刷新。解決這個問題的辦法就是,查看 html 的源代碼。
對于復雜的,不能查到原因的頁面,可以通過 w3c 的頁面校驗程序 http://validator.w3.org/ 來分析。
如果沒有定義變量,或者變量定義錯誤也會讓程序變得古怪。例如下面的死循環:
for($counter=0; $counter<10; $counter++)
myfunction();
?>
變量 $counter 在增加,而 $counter 永遠小于 10。這類錯誤一般都能通過設置較高的錯誤報告級別來找到:
error_reporting(e_all);
for($counter=0; $counter<10; $counter++)
myfunction();
?>
5. 采用 header() 函數處理單部件查詢
在很多 web 數據庫應用中,一些功能往往讓用戶點擊一個連接后,繼續停留在當前頁面,這樣的工作我叫它“單部件查詢”。
下面是一個叫做 calling.php 的腳本:
"-//w3c//dtd html 4.0 transitional//en"
"http://www.w3.org/tr/html4/loose.dtd" >
click here!
當用戶點擊上面的連接時,就去調用 action.php。下面是 action.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:
$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);
?>
"-//w3c//dtd html 4.0 transitional//en"
"http://www.w3.org/tr/html4/loose.dtd" >
i've inserted the customer for you.
?>
假設我們用下面的連接使用這個程序:
http://www.freelamp.com/addcust.php?surname=smith&firstname=fred
如果這個請求只提交一次,ok ,不會有問題,但是如果多次刷新,你就會有多條記錄插入。
這個問題可以通過 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");
?>
這個腳本把瀏覽器重定向到一個新的頁面:cust_receipt.php:
"-//w3c//dtd html 4.0 transitional//en"
"http://www.w3.org/tr/html4/loose.dtd" >
i've inserted the customer for you.
這樣,原來的頁面繼續刷新也沒有副作用了。
7. 巧用鎖機制來提高應用性能
如果我們要緊急運行一個報表,那么,我們可以對表加寫鎖,防治別人讀寫,來提高對這個表的處理速度。
8. 用 mysql_unbuffered_query() 開發快速的腳本
這個函數能用來替換 mysql_query() 函數,主要的區別就是 mysql_unbuffered_query() 執行完查詢后馬上返回,不需要等待或者對數據庫加鎖。
但是返回的行數不能用mysql_num_rows() 函數來檢查,因為輸出的結果集大小未知。