1.
複製代碼 代碼如下:
$connection=mysql_connect("localhost","root","password"); //串連並選擇資料庫伺服器
mysql_select_db("test",$connection);
$query="insert into users(user_name)"; //在test資料庫裡插入一條資料
$query.="values('tuxiaohui')";
$result=mysql_query($query);
if(!$query)
echo "insert data failed!
";
else{
$query="select * from users"; //查詢資料
$result=mysql_query($query,$connection);
for($rows_count=0;$rows_count<7;$rows_count++) //用mysql_result獲得資料並輸出,mysql_result() 返回 MySQL 結果集中一個單元的內容。
{
echo "使用者ID:".mysql_result($result,$rows_count,"user_id")."
";
echo "使用者名稱:".mysql_result($result,$rows_count,"user_name")."
";
}
}
?>
2.
複製代碼 代碼如下:
$connection=mysql_connect("localhost","root","password"); //串連並選擇資料庫伺服器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_row($result))
{
echo "使用者ID:".$row[0]."
";
echo "使用者名稱:".$row[1]."
";
}
?>
3.
複製代碼 代碼如下:
$connection=mysql_connect("localhost","root","password"); //串連並選擇資料庫伺服器
mysql_select_db("test",$connection);
$query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_array($result))
{
echo "使用者ID:".$row[0]."
"; //也可以寫做$row["user_id"]
echo "使用者名稱:".$row[1]."
"; //也可以寫做$row["user_name"]
}
?>
4.
複製代碼 代碼如下:
$connection=mysql_connect("localhost","root","root"); //串連並選擇資料庫伺服器
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."
"; //通過對象運算子->獲得改行資料在其屬性上的值。
echo "使用者名稱:".$row->user_name."
";
}
?>
5.綜合比較:
mysql_result():優點在於使用方便;其缺點在於功能少,一次調用只能擷取結果資料集中的一行元素,對較大型的資料庫效率較低;
mysql_fetch_row():優點在於執行效率在4種方法中最高;不足在於只能用數字作為屬性索引來獲得屬性值,在使用時非常容易出現混淆;
mysql_fetch_array():執行效率同樣高,同mysql_fetch_row()相差無幾,並界可以用屬性名稱方式直接獲得屬性值,因此在實際應用中最常用;
mysql_fetch_object():採用了物件導向思想,在設計思路上更為先進,如果習慣於用物件導向的思路來寫程式,則會很自地選擇它。其次,該方法的優點還體現在,對於結構較為負責的資料結果,在邏輯上更為清晰。
http://www.bkjia.com/PHPjc/322709.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/322709.htmlTechArticle1.!--使用mysql_result()來擷取資料-- 複製代碼 代碼如下: ?php $connection=mysql_connect("localhost","root","password"); //串連並選擇資料庫伺服器 mysql_selec...