<?php $temp[0] = "richmond"; $temp[1] = "tigers"; $temp[2] = "pre " /> 亚洲成人999,人人视频精品,精品免费av

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

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

1. php 中數(shù)組的使用  
在操作數(shù)據(jù)庫時(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è)很簡單的:  

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

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

但是如果后面那個(gè) echo 語句沒有雙引號(hào)引起來的話,就要報(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ò)問題是:  

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,頁面就不能刷新。解決這個(gè)問題的辦法就是,查看 HTML 的源代碼。  

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

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

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

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

<?php  
error_reporting(E_ALL);  

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

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

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

下面是一個(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ù)庫功能  

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

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


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

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

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

6. reload 的問題及其解決  
我以前在寫 php 程序時(shí),經(jīng)常碰到頁面刷新時(shí),數(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è)我們用下面的連接使用這個(gè)程序:  

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

如果這個(gè)請(qǐng)求只提交一次,OK ,不會(huì)有問題,但是如果多次刷新,你就會(huì)有多條記錄插入。  
這個(gè)問題可以通過 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è)新的頁面: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. 巧用鎖機(jī)制來提高應(yīng)用性能  
如果我們要緊急運(yùn)行一個(gè)報(bào)表,那么,我們可以對(duì)表加寫鎖,防治別人讀寫,來提高對(duì)這個(gè)表的處理速度。  

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

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

php技術(shù)PHP 和 MySQL 開發(fā)的 8 個(gè)技巧,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 罗平县| 义马市| 双桥区| 广汉市| 阿克陶县| 齐河县| 和政县| 西畴县| 珲春市| 高淳县| 陕西省| 郯城县| 通海县| 建昌县| 洛川县| 环江| 鄂尔多斯市| 长治县| 通许县| 色达县| 湘乡市| 栾川县| 郓城县| 怀仁县| 九台市| 新巴尔虎右旗| 略阳县| 永泰县| 武山县| 通江县| 永定县| 澎湖县| 辽源市| 荥阳市| 金川县| 新乡县| 惠来县| 华池县| 札达县| 秦皇岛市| 宣武区|