PHP 和 MySQL 開發(fā)的 8 個技巧
更新時間:2007年01月02日 00:00:00 作者:
1. PHP 中數(shù)組的使用
在操作數(shù)據(jù)庫時,使用關(guān)聯(lián)數(shù)組(associatively-indexed arrays)十分有幫助,下面我們看一個基本的數(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 還能輸出文字下標:
<?php
$temp = array("club" => "richmond",
"nickname" =>"tigers",
"aim" => "premiers");
foreach ($temp as $key => $value)
echo "$key : $value ";
?>
PHP 手冊中描述了大約 50 個用于處理數(shù)組的函數(shù)。
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. 采用關(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ù)組,可以同時用兩種方式引用,例如 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"]。
聚集函數(shù)的引用就是引用名:
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
出現(xiàn)這些情況的大多數(shù)原因并不在于腳本的邏輯,而是 HTML 中存在的 bug 或者腳本生成的 HTML 的 bug 。例如缺少類似 </table>, </form>, </frame> 之類的關(guān)閉 Tag,頁面就不能刷新。解決這個問題的辦法就是,查看 HTML 的源代碼。
對于復(fù)雜的,不能查到原因的頁面,可以通過 W3C 的頁面校驗程序 http://validator.w3.org/ 來分析。
如果沒有定義變量,或者變量定義錯誤也會讓程序變得古怪。例如下面的死循環(huán):
<?php
for($counter=0; $counter<10; $Counter++)
myFunction();
?>
變量 $Counter 在增加,而 $counter 永遠小于 10。這類錯誤一般都能通過設(shè)置較高的錯誤報告級別來找到:
<?php
error_reporting(E_ALL);
for($counter=0; $counter<10; $Counter++)
myFunction();
?>
5. 采用 header() 函數(shù)處理單部件查詢
在很多 Web 數(shù)據(jù)庫應(yīng)用中,一些功能往往讓用戶點擊一個連接后,繼續(xù)停留在當前頁面,這樣的工作我叫它“單部件查詢”。
下面是一個叫做 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>
當用戶點擊上面的連接時,就去調(diào)用 action.php。下面是 action.php 的源碼:
<?php
// 數(shù)據(jù)庫功能
// 重定向
header("Location: $HTTP_REFERER");
exit;
?>
這里有兩個常見的錯誤需要提醒一下:
調(diào)用 header() 函數(shù)后要包含一個 exit 語句讓腳本停止,否則后續(xù)的腳本可能會在頭發(fā)送前輸出。
header() 函數(shù)常見的一個錯誤是:
Warning: Cannot add header information - headers already sent...
header() 函數(shù)只能在 HTML 輸出之前被調(diào)用,因此你需要檢查 php 前面可能存在的空行,空格等等。
6. reload 的問題及其解決
我以前在寫 PHP 程序時,經(jīng)常碰到頁面刷新時,數(shù)據(jù)庫多處理一次的情況。
我們來看 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è)我們用下面的連接使用這個程序:
http://www.freelamp.com/addcust. ... &firstname=Fred
如果這個請求只提交一次,OK ,不會有問題,但是如果多次刷新,你就會有多條記錄插入。
這個問題可以通過 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");
?>
這個腳本把瀏覽器重定向到一個新的頁面: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>
這樣,原來的頁面繼續(xù)刷新也沒有副作用了。
7. 巧用鎖機制來提高應(yīng)用性能
如果我們要緊急運行一個報表,那么,我們可以對表加寫鎖,防治別人讀寫,來提高對這個表的處理速度。
8. 用 mysql_unbuffered_query() 開發(fā)快速的腳本
這個函數(shù)能用來替換 mysql_query() 函數(shù),主要的區(qū)別就是 mysql_unbuffered_query() 執(zhí)行完查詢后馬上返回,不需要等待或者對數(shù)據(jù)庫加鎖。
但是返回的行數(shù)不能用mysql_num_rows() 函數(shù)來檢查,因為輸出的結(jié)果集大小未知。
在操作數(shù)據(jù)庫時,使用關(guān)聯(lián)數(shù)組(associatively-indexed arrays)十分有幫助,下面我們看一個基本的數(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 還能輸出文字下標:
<?php
$temp = array("club" => "richmond",
"nickname" =>"tigers",
"aim" => "premiers");
foreach ($temp as $key => $value)
echo "$key : $value ";
?>
PHP 手冊中描述了大約 50 個用于處理數(shù)組的函數(shù)。
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. 采用關(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ù)組,可以同時用兩種方式引用,例如 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"]。
聚集函數(shù)的引用就是引用名:
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
出現(xiàn)這些情況的大多數(shù)原因并不在于腳本的邏輯,而是 HTML 中存在的 bug 或者腳本生成的 HTML 的 bug 。例如缺少類似 </table>, </form>, </frame> 之類的關(guān)閉 Tag,頁面就不能刷新。解決這個問題的辦法就是,查看 HTML 的源代碼。
對于復(fù)雜的,不能查到原因的頁面,可以通過 W3C 的頁面校驗程序 http://validator.w3.org/ 來分析。
如果沒有定義變量,或者變量定義錯誤也會讓程序變得古怪。例如下面的死循環(huán):
<?php
for($counter=0; $counter<10; $Counter++)
myFunction();
?>
變量 $Counter 在增加,而 $counter 永遠小于 10。這類錯誤一般都能通過設(shè)置較高的錯誤報告級別來找到:
<?php
error_reporting(E_ALL);
for($counter=0; $counter<10; $Counter++)
myFunction();
?>
5. 采用 header() 函數(shù)處理單部件查詢
在很多 Web 數(shù)據(jù)庫應(yīng)用中,一些功能往往讓用戶點擊一個連接后,繼續(xù)停留在當前頁面,這樣的工作我叫它“單部件查詢”。
下面是一個叫做 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>
當用戶點擊上面的連接時,就去調(diào)用 action.php。下面是 action.php 的源碼:
<?php
// 數(shù)據(jù)庫功能
// 重定向
header("Location: $HTTP_REFERER");
exit;
?>
這里有兩個常見的錯誤需要提醒一下:
調(diào)用 header() 函數(shù)后要包含一個 exit 語句讓腳本停止,否則后續(xù)的腳本可能會在頭發(fā)送前輸出。
header() 函數(shù)常見的一個錯誤是:
Warning: Cannot add header information - headers already sent...
header() 函數(shù)只能在 HTML 輸出之前被調(diào)用,因此你需要檢查 php 前面可能存在的空行,空格等等。
6. reload 的問題及其解決
我以前在寫 PHP 程序時,經(jīng)常碰到頁面刷新時,數(shù)據(jù)庫多處理一次的情況。
我們來看 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è)我們用下面的連接使用這個程序:
http://www.freelamp.com/addcust. ... &firstname=Fred
如果這個請求只提交一次,OK ,不會有問題,但是如果多次刷新,你就會有多條記錄插入。
這個問題可以通過 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");
?>
這個腳本把瀏覽器重定向到一個新的頁面: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>
這樣,原來的頁面繼續(xù)刷新也沒有副作用了。
7. 巧用鎖機制來提高應(yīng)用性能
如果我們要緊急運行一個報表,那么,我們可以對表加寫鎖,防治別人讀寫,來提高對這個表的處理速度。
8. 用 mysql_unbuffered_query() 開發(fā)快速的腳本
這個函數(shù)能用來替換 mysql_query() 函數(shù),主要的區(qū)別就是 mysql_unbuffered_query() 執(zhí)行完查詢后馬上返回,不需要等待或者對數(shù)據(jù)庫加鎖。
但是返回的行數(shù)不能用mysql_num_rows() 函數(shù)來檢查,因為輸出的結(jié)果集大小未知。
相關(guān)文章
docker?中搭建php環(huán)境經(jīng)驗分享
這篇文章主要介紹了docker?中搭建php環(huán)境經(jīng)驗分享的相關(guān)資料,需要的朋友可以參考下2023-09-09關(guān)于Appserv無法打開localhost問題的解決方法
安裝了Appserv時,無法打開http://localhost或是http://127.0.0.1 下面的具體的解決方法,大家可以參考下。多注意看下端口占用問題。2009-10-10php在服務(wù)器執(zhí)行exec命令失敗的解決方法
出于安全的原因,服務(wù)器是不允許php或者其他語言執(zhí)行exec命令的,當你有特殊需要php在服務(wù)器執(zhí)行exec命令時,你需要設(shè)置兩個地方,不然就無法執(zhí)行成功2012-03-03PHP管理內(nèi)存函數(shù) memory_get_usage()使用介紹
我們在實際編碼中,要想實現(xiàn)對內(nèi)存的查看和操作,許多程序員們第一個想到的就是PHP memory_get_usage()這個PHP腳本內(nèi)存函數(shù)2012-09-09通過PHP的內(nèi)置函數(shù),通過DES算法對數(shù)據(jù)加密和解密
數(shù)據(jù)加密的基本過程就是對原來為明文的文件或數(shù)據(jù)按某種算法進行處理,使其成為不可讀的一段代碼,通常稱為密文,使其只能在輸入相應(yīng)的密鑰之后才能顯示出本來內(nèi)容,通過這樣的途徑來達到保護數(shù)據(jù)不被非法人竊取、閱讀的目的2012-06-06