欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP 和 MySQL 開發(fā)的 8 個(gè)技巧

 更新時(shí)間:2007年01月02日 00:00:00   作者:  
1. PHP 中數(shù)組的使用  
在操作數(shù)據(jù)庫(kù)時(shí),使用關(guān)聯(lián)數(shù)組(associatively-indexed arrays)十分有幫助,下面我們看一個(gè)基本的數(shù)字格式的數(shù)組遍歷:  

<?php  
$temp[0] = "richmond";  
$temp[1] = "tigers";  
$temp[2] = "premiers";  

for($x=0;$x<count($temp);$x++)  
{  
echo $temp[$x];  
echo " ";  
}  
?>  

然而另外一種更加節(jié)省代碼的方式是:  

<?php  
$temp = array("richmond", "tigers", "premiers");  
foreach ($temp as $element)  
echo "$element ";  
?>  

foreach 還能輸出文字下標(biāo):  

<?php  
$temp = array("club" => "richmond",  
"nickname" =>"tigers",  
"aim" => "premiers");  

foreach ($temp as $key => $value)  
echo "$key : $value ";  
?>  
PHP 手冊(cè)中描述了大約 50 個(gè)用于處理數(shù)組的函數(shù)。  

2. 在 PHP 字符串中加入變量  

這個(gè)很簡(jiǎn)單的:  

<?php  
$temp = "hello"  
echo "$temp world";  
?>  

但是需要說(shuō)明的是,盡管下面的例子沒(méi)有錯(cuò)誤:  
<?php  
$temp = array("one" => 1, "two" => 2);  
// 輸出:: The first element is 1  
echo "The first element is $temp[one].";  
?>  

但是如果后面那個(gè) echo 語(yǔ)句沒(méi)有雙引號(hào)引起來(lái)的話,就要報(bào)錯(cuò),因此建議使用花括號(hào):  

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


3. 采用關(guān)聯(lián)數(shù)組存取查詢結(jié)果  
看下面的例子:  

<?php  
$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";  
}  
?>  

函數(shù) mysql_fetch_array() 把查詢結(jié)果的一行放入數(shù)組,可以同時(shí)用兩種方式引用,例如 cust_id 可以同時(shí)用下面兩種方式:$row["cust_id"] 或者$row[0] 。顯然,前者的可讀性要比后者好多了。  

在多表連查中,如果兩個(gè)列名字一樣,最好用別名分開:  

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"]。  

聚集函數(shù)的引用就是引用名:  

SELECT count(*)  
FROM customer;  

列名的引用為: $row["count(*)"]。  

4. 注意常見的 PHP bug  

常見的 PHP 糾錯(cuò)問(wèn)題是:  

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  

出現(xiàn)這些情況的大多數(shù)原因并不在于腳本的邏輯,而是 HTML 中存在的 bug 或者腳本生成的 HTML 的 bug 。例如缺少類似 </table>, </form>, </frame> 之類的關(guān)閉 Tag,頁(yè)面就不能刷新。解決這個(gè)問(wèn)題的辦法就是,查看 HTML 的源代碼。  

對(duì)于復(fù)雜的,不能查到原因的頁(yè)面,可以通過(guò) W3C 的頁(yè)面校驗(yàn)程序 http://validator.w3.org/ 來(lái)分析。  

如果沒(méi)有定義變量,或者變量定義錯(cuò)誤也會(huì)讓程序變得古怪。例如下面的死循環(huán):  

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

變量 $Counter 在增加,而 $counter 永遠(yuǎn)小于 10。這類錯(cuò)誤一般都能通過(guò)設(shè)置較高的錯(cuò)誤報(bào)告級(jí)別來(lái)找到:  

<?php  
error_reporting(E_ALL);  

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

5. 采用 header() 函數(shù)處理單部件查詢  

在很多 Web 數(shù)據(jù)庫(kù)應(yīng)用中,一些功能往往讓用戶點(diǎn)擊一個(gè)連接后,繼續(xù)停留在當(dāng)前頁(yè)面,這樣的工作我叫它“單部件查詢”。  

下面是一個(gè)叫做 calling.php 的腳本:  

<!DOCTYPE HTML PUBLIC  
"-//W3C//DTD HTML 4.0 Transitional//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>  

當(dāng)用戶點(diǎn)擊上面的連接時(shí),就去調(diào)用 action.php。下面是 action.php 的源碼:  

<?php  
// 數(shù)據(jù)庫(kù)功能  

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

這里有兩個(gè)常見的錯(cuò)誤需要提醒一下:  
調(diào)用 header() 函數(shù)后要包含一個(gè) exit 語(yǔ)句讓腳本停止,否則后續(xù)的腳本可能會(huì)在頭發(fā)送前輸出。  


header() 函數(shù)常見的一個(gè)錯(cuò)誤是:  

Warning: Cannot add header information - headers already sent...  

header() 函數(shù)只能在 HTML 輸出之前被調(diào)用,因此你需要檢查 php 前面可能存在的空行,空格等等。  

6. reload 的問(wèn)題及其解決  
我以前在寫 PHP 程序時(shí),經(jīng)常碰到頁(yè)面刷新時(shí),數(shù)據(jù)庫(kù)多處理一次的情況。  
我們來(lái)看 addcust.php:  

<?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);  
?>  
<!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>  
?>  
假設(shè)我們用下面的連接使用這個(gè)程序:  

http://www.freelamp.com/addcust. ... &firstname=Fred  

如果這個(gè)請(qǐng)求只提交一次,OK ,不會(huì)有問(wèn)題,但是如果多次刷新,你就會(huì)有多條記錄插入。  
這個(gè)問(wèn)題可以通過(guò) header() 函數(shù)解決:下面是新版本的 addcust.php:  

<?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");  
?>  
這個(gè)腳本把瀏覽器重定向到一個(gè)新的頁(yè)面: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>  
這樣,原來(lái)的頁(yè)面繼續(xù)刷新也沒(méi)有副作用了。  

7. 巧用鎖機(jī)制來(lái)提高應(yīng)用性能  
如果我們要緊急運(yùn)行一個(gè)報(bào)表,那么,我們可以對(duì)表加寫鎖,防治別人讀寫,來(lái)提高對(duì)這個(gè)表的處理速度。  

8. 用 mysql_unbuffered_query() 開發(fā)快速的腳本  
這個(gè)函數(shù)能用來(lái)替換 mysql_query() 函數(shù),主要的區(qū)別就是 mysql_unbuffered_query() 執(zhí)行完查詢后馬上返回,不需要等待或者對(duì)數(shù)據(jù)庫(kù)加鎖。  

但是返回的行數(shù)不能用mysql_num_rows() 函數(shù)來(lái)檢查,因?yàn)檩敵龅慕Y(jié)果集大小未知。

相關(guān)文章

最新評(píng)論