PHP開發(fā)中四種查詢返回結(jié)果分析
更新時間:2011年01月02日 00:45:58 作者:
PHP開發(fā)中四種查詢返回結(jié)果分析,需要的朋友可以參考下。
1.<!--使用mysql_result()來獲取數(shù)據(jù)-->
<?php
$connection=mysql_connect("localhost","root","password"); //連接并選擇數(shù)據(jù)庫服務(wù)器
mysql_select_db("test",$connection);
$query="insert into users(user_name)"; //在test數(shù)據(jù)庫里插入一條數(shù)據(jù)
$query.="values('tuxiaohui')";
$result=mysql_query($query);
if(!$query)
echo "insert data failed!<br>";
else{
$query="select * from users"; //查詢數(shù)據(jù)
$result=mysql_query($query,$connection);
for($rows_count=0;$rows_count<7;$rows_count++) //用mysql_result獲得數(shù)據(jù)并輸出,mysql_result() 返回 MySQL 結(jié)果集中一個單元的內(nèi)容。
{
echo "用戶ID:".mysql_result($result,$rows_count,"user_id")."<br>";
echo "用戶名:".mysql_result($result,$rows_count,"user_name")."<br>";
}
}
?>
2.<!--使用mysql_fetch_row()來獲取數(shù)據(jù),以數(shù)組的形式返回查詢結(jié)果-->
<?php
$connection=mysql_connect("localhost","root","password"); //連接并選擇數(shù)據(jù)庫服務(wù)器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_row($result))
{
echo "用戶ID:".$row[0]."<br>";
echo "用戶名:".$row[1]."<br>";
}
?>
3.<!--使用mysql_fetch_array()來獲取數(shù)據(jù),同mysql_fetch_row()類似,也是獲取結(jié)果集中當(dāng)前行數(shù)據(jù),并在調(diào)用后自動滑向下一行-->
<?php
$connection=mysql_connect("localhost","root","password"); //連接并選擇數(shù)據(jù)庫服務(wù)器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_array($result))
{
echo "用戶ID:".$row[0]."<br>"; //也可以寫做$row["user_id"]
echo "用戶名:".$row[1]."<br>"; //也可以寫做$row["user_name"]
}
?>
4.<!--使用mysql_fetch_object()以對象的形式返回查詢結(jié)果,也是用于查詢數(shù)據(jù)結(jié)果集,返回當(dāng)前行數(shù)據(jù),并自動滑向下一行,不同的是它返回的是一個對象,這個對象的屬性集合即為數(shù)據(jù)的屬性集合,而屬性上的值則為數(shù)據(jù)庫中當(dāng)前行該屬性上的值-->
<?php
$connection=mysql_connect("localhost","root","root"); //連接并選擇數(shù)據(jù)庫服務(wù)器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_object($result))
{
echo "用戶ID:".$row->user_id."<br>"; //通過對象運(yùn)算符->獲得改行數(shù)據(jù)在其屬性上的值。
echo "用戶名:".$row->user_name."<br>";
}
?>
5.綜合比較:
mysql_result():優(yōu)點(diǎn)在于使用方便;其缺點(diǎn)在于功能少,一次調(diào)用只能獲取結(jié)果數(shù)據(jù)集中的一行元素,對較大型的數(shù)據(jù)庫效率較低;
mysql_fetch_row():優(yōu)點(diǎn)在于執(zhí)行效率在4種方法中最高;不足在于只能用數(shù)字作為屬性索引來獲得屬性值,在使用時非常容易出現(xiàn)混淆;
mysql_fetch_array():執(zhí)行效率同樣高,同mysql_fetch_row()相差無幾,并界可以用屬性名方式直接獲得屬性值,因此在實(shí)際應(yīng)用中最常用;
mysql_fetch_object():采用了面向?qū)ο笏枷耄谠O(shè)計(jì)思路上更為先進(jìn),如果習(xí)慣于用面向?qū)ο蟮乃悸穪韺懗绦颍瑒t會很自地選擇它。其次,該方法的優(yōu)點(diǎn)還體現(xiàn)在,對于結(jié)構(gòu)較為負(fù)責(zé)的數(shù)據(jù)結(jié)果,在邏輯上更為清晰。
復(fù)制代碼 代碼如下:
<?php
$connection=mysql_connect("localhost","root","password"); //連接并選擇數(shù)據(jù)庫服務(wù)器
mysql_select_db("test",$connection);
$query="insert into users(user_name)"; //在test數(shù)據(jù)庫里插入一條數(shù)據(jù)
$query.="values('tuxiaohui')";
$result=mysql_query($query);
if(!$query)
echo "insert data failed!<br>";
else{
$query="select * from users"; //查詢數(shù)據(jù)
$result=mysql_query($query,$connection);
for($rows_count=0;$rows_count<7;$rows_count++) //用mysql_result獲得數(shù)據(jù)并輸出,mysql_result() 返回 MySQL 結(jié)果集中一個單元的內(nèi)容。
{
echo "用戶ID:".mysql_result($result,$rows_count,"user_id")."<br>";
echo "用戶名:".mysql_result($result,$rows_count,"user_name")."<br>";
}
}
?>
2.<!--使用mysql_fetch_row()來獲取數(shù)據(jù),以數(shù)組的形式返回查詢結(jié)果-->
復(fù)制代碼 代碼如下:
<?php
$connection=mysql_connect("localhost","root","password"); //連接并選擇數(shù)據(jù)庫服務(wù)器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_row($result))
{
echo "用戶ID:".$row[0]."<br>";
echo "用戶名:".$row[1]."<br>";
}
?>
3.<!--使用mysql_fetch_array()來獲取數(shù)據(jù),同mysql_fetch_row()類似,也是獲取結(jié)果集中當(dāng)前行數(shù)據(jù),并在調(diào)用后自動滑向下一行-->
復(fù)制代碼 代碼如下:
<?php
$connection=mysql_connect("localhost","root","password"); //連接并選擇數(shù)據(jù)庫服務(wù)器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_array($result))
{
echo "用戶ID:".$row[0]."<br>"; //也可以寫做$row["user_id"]
echo "用戶名:".$row[1]."<br>"; //也可以寫做$row["user_name"]
}
?>
4.<!--使用mysql_fetch_object()以對象的形式返回查詢結(jié)果,也是用于查詢數(shù)據(jù)結(jié)果集,返回當(dāng)前行數(shù)據(jù),并自動滑向下一行,不同的是它返回的是一個對象,這個對象的屬性集合即為數(shù)據(jù)的屬性集合,而屬性上的值則為數(shù)據(jù)庫中當(dāng)前行該屬性上的值-->
復(fù)制代碼 代碼如下:
<?php
$connection=mysql_connect("localhost","root","root"); //連接并選擇數(shù)據(jù)庫服務(wù)器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_object($result))
{
echo "用戶ID:".$row->user_id."<br>"; //通過對象運(yùn)算符->獲得改行數(shù)據(jù)在其屬性上的值。
echo "用戶名:".$row->user_name."<br>";
}
?>
5.綜合比較:
mysql_result():優(yōu)點(diǎn)在于使用方便;其缺點(diǎn)在于功能少,一次調(diào)用只能獲取結(jié)果數(shù)據(jù)集中的一行元素,對較大型的數(shù)據(jù)庫效率較低;
mysql_fetch_row():優(yōu)點(diǎn)在于執(zhí)行效率在4種方法中最高;不足在于只能用數(shù)字作為屬性索引來獲得屬性值,在使用時非常容易出現(xiàn)混淆;
mysql_fetch_array():執(zhí)行效率同樣高,同mysql_fetch_row()相差無幾,并界可以用屬性名方式直接獲得屬性值,因此在實(shí)際應(yīng)用中最常用;
mysql_fetch_object():采用了面向?qū)ο笏枷耄谠O(shè)計(jì)思路上更為先進(jìn),如果習(xí)慣于用面向?qū)ο蟮乃悸穪韺懗绦颍瑒t會很自地選擇它。其次,該方法的優(yōu)點(diǎn)還體現(xiàn)在,對于結(jié)構(gòu)較為負(fù)責(zé)的數(shù)據(jù)結(jié)果,在邏輯上更為清晰。
相關(guān)文章
關(guān)于php程序報(bào)date()警告的處理(date_default_timezone_set)
PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function2013-10-10
解決更換PHP5.4以上版本后Dedecms后臺登錄空白問題的方法
為什么會出現(xiàn)更換PHP5.4以上版本后Dedecms后臺登錄空白的情況,本文將給大家詳細(xì)分析,找出真正原因以及解決辦法。2015-10-10

