如果用了 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>"; } ?>
綜合比較
本節主要介紹了獲取查詢結果集的4個函數,此處對它們進行綜合比較。
● mysql_result():優點在于使用方便;而缺點在于功能少,一次調用只能獲取結果數據集中的一行記錄,對較大型的數據庫效率較低。
● mysql_fetch_row():優點在于執行效率在4種方法中最高;不足在于只能用數字作為屬性索引來獲得屬性值,在使用時非常容易出現混淆。
● mysql_fetch_array():執行效率同樣很高,同mysql_fetch_row()相差無幾,并且可以用屬性名方式直接獲取得屬性值,因此,在實際應用中最常用。
● mysql_fetch_object():采用了面向對象的思想,在設計思路上更為先進,如果讀者習慣于面向對象的思路來寫程序,則會很自然的選擇它。其次,該方法的優點還體現在,對于結構較為復雜的數據結果,在邏輯上顯得更為清晰。
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。