php操作mysql獲取select 結果的幾種方法
如果用了 MYSQL_BOTH,將得到一個同時包含關聯和數字索引的數組。
用 MYSQL_ASSOC 只得到關聯索引(如同mysql_fetch_assoc() 那樣),
用 MYSQL_NUM 只得到數字索引(如同 mysql_fetch_row 那樣)。
1. mysql_fetch_array($rs,MYSQL_ASSOC)
[@test01 model]# php test.php Array ( [name] => hellokitty [addr] => i dont kno ) [@test01 model]# more test.php <?php $link=mysql_connect("10.12.136.181","hello","hello"); if(!$link) echo "沒有連接成功!"; mysql_select_db("hhhhh", $link); $q = "SELECT * FROM hello"; mysql_query("SET NAMES GB2312"); $rs = mysql_query($q); if(!$rs) { die("Valid result!"); } $result=mysql_fetch_array($rs,MYSQL_ASSOC); print_r($result); mysql_free_result($rs); ?>
2.mysql_fetch_array($rs,MYSQL_BOTH);獲取數組
[@test01 model]# more test.php <?php $link=mysql_connect("10.12.136.181","hello","hello"); if(!$link) echo "沒有連接成功!"; mysql_select_db("hhhhh", $link); $q = "SELECT * FROM hello"; mysql_query("SET NAMES GB2312"); $rs = mysql_query($q); if(!$rs) { die("Valid result!"); } $result=mysql_fetch_array($rs,MYSQL_ASSOC); print_r($result); mysql_free_result($rs); ?> [@test01 model]# vim test.php [@test01 model]# php test.php Array ( [0] => hellokitty [name] => hellokitty [1] => i dont kno [addr] => i dont kno ) [@test01 model]#
3.mysql_fetch_array($rs,MYSQL_NUM) 獲取數組
[@test01 model]# php test.php Array ( [0] => hellokitty [1] => i dont kno ) [@test01 model]# more test.php <?php $link=mysql_connect("10.12.136.181","hello","hello"); if(!$link) echo "沒有連接成功!"; mysql_select_db("hhhhh", $link); $q = "SELECT * FROM hello"; mysql_query("SET NAMES GB2312"); $rs = mysql_query($q); if(!$rs) { die("Valid result!"); } $result=mysql_fetch_array($rs,MYSQL_NUM); print_r($result); mysql_free_result($rs); ?> [@test01 model]#
下面是補充:
php獲取結果集的幾個方法
<?php $conn=mysql_connect("localhost","root",""); $select=mysql_select_db("books",$conn); $query="insert into computers(name,price,publish_data) "; $query.="values('JSP',28.00,'2008-11-1')"; $query="select * from computers"; $result=mysql_query($query); //以下是使用mysql_result()函數來獲取到查詢結果 $num=mysql_num_rows($result); for($rows_count=0;$rows_count<$num;$rows_count++){ echo "書名:".mysql_result($result,$rows_count,"name"); echo "價格:".mysql_result($result,$rows_count,"price"); echo "出版日期:".mysql_result($result,$rows_count,"publish_data")."<br>"; } //以下是使用mysql_fetch_row()函數來獲取到查詢結果 while($row=mysql_fetch_row($result)) { echo "書號:".$row[0]."<br>"; echo "書名:".$row[1]."<br>"; echo "價格:".$row[2]."<br>"; echo "出版日期:".$row[3]."<br>"; echo "<br>"; } //以下是使用mysql_fetch_array()函數來獲取到查詢結果 while($row=mysql_fetch_array($result)) { echo "書號:".$row[0]."<br>"; echo "書名:".$row[1]."<br>"; echo "價格:".$row["price"]."<br>"; echo "出版日期:".$row["publish_data"]."<br>"; echo "<br>"; } //mysql_fetch_assoc()同mysql_fetch_array($result,MYSQL_ASSOC)一樣 while($row = mysql_fetch_assoc($res)){ echo $row['price'].'::'.$row['publish_data'].”; } //$row[0]不能取值 //以下是使用mysql_fetch_object()函數來獲取到查詢結果 while($row=mysql_fetch_object($result)) { echo "書號:".$row->id."<br>"; echo "書名:".$row->name."<br>"; echo "價格:".$row->price."<br>"; echo "出版日期:".$row->publish_data."<br>"; echo "<br>"; } ?>
綜合比較
本節(jié)主要介紹了獲取查詢結果集的4個函數,此處對它們進行綜合比較。
● mysql_result():優(yōu)點在于使用方便;而缺點在于功能少,一次調用只能獲取結果數據集中的一行記錄,對較大型的數據庫效率較低。
● mysql_fetch_row():優(yōu)點在于執(zhí)行效率在4種方法中最高;不足在于只能用數字作為屬性索引來獲得屬性值,在使用時非常容易出現混淆。
● mysql_fetch_array():執(zhí)行效率同樣很高,同mysql_fetch_row()相差無幾,并且可以用屬性名方式直接獲取得屬性值,因此,在實際應用中最常用。
● mysql_fetch_object():采用了面向對象的思想,在設計思路上更為先進,如果讀者習慣于面向對象的思路來寫程序,則會很自然的選擇它。其次,該方法的優(yōu)點還體現在,對于結構較為復雜的數據結果,在邏輯上顯得更為清晰。
后3個函數的共同點在于,都是取得當前行的數據,然后自動滑向后一行。有時候,希望控制滑動的行數,這是常常搭配使用的一個函數是mysql_data_seek(),其定義為:
int mysql_data_seek(int result_identifier,int row_number)
調用該函數可以在結果集中向后滑動row_number行,在下一次調用mysql_fetch_*函數時,讀取的將是向后滑動row_number行后的記錄。
相關文章
PHP函數preg_match_all正則表達式的基本使用詳細解析
以下是對PHP中的函數preg_match_all正則表達式的基本使用進行了詳細的分析介紹,需要的朋友可以過來參考下2013-08-08php中使用key,value,current,next和prev函數遍歷數組的方法
這篇文章主要介紹了php中使用key,value,current,next和prev函數遍歷數組的方法,較為詳細的分析了php中數組遍歷的常用技巧與實例用法,需要的朋友可以參考下2015-03-03