php輸出mysqli查詢出來的結果


php連接mysql我有文章已經寫過了,這篇文章主要是介紹從mysql中查詢出結果之后怎么輸出的問題。 
一:mysqli_fetch_row(); 
查詢結果:array([0]=>小王) 
查詢:

[php]  view plain  copy
 
  1. while ($row = mysqli_fetch_assoc($result)) {   
  2. $memberlist = $row[0];   
  3. }//end while()  

 

二:mysqli_fetch_assos(); 

查詢結果:array([name]=>小王) 
查詢:

 

[php]  view plain  copy
 
  1. while ($row = mysqli_fetch_assoc($result)) {   
  2. $memberlist = $row['memberlist'];   
  3. }//end while()  


三、mysqli_fetch_array(); 

 

查詢結果:array([0]=>小王 [name]=>小王) 
查詢:

 

[php]  view plain  copy
 
  1. while ($row = mysqli_fetch_assoc($result)) {   
  2. $memberlist = $row['memberlist'];   
  3. $memberlist = $row[0];   
  4. }//end while()  

 

四、fetch_array(); 
查詢結果:array([0]=>小王 [name]=>小王) 
查詢:

 

[php]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. $sql = "select * from user";    
  2. $result = $conn->query($sql);    
  3.     
  4. if ($result)     
  5. {    
  6.     if ($result->num_rows>0)    
  7.     {    
  8.         while ($rows = $result->fetch_array()) {    
  9.             print_r($rows);    
  10.             echo "<BR>rows['id']:".$rows['id'];    
  11.             echo "<BR>rows['name']:".$rows['name'];    
  12.             echo "<BR>rows['pwd']:".$rows['pwd'];    
  13.         }//end while()    
  14.     }else{    
  15.         echo "<BR>查詢結果為空!";       
  16.     }//end if()    
  17. }else{    
  18.     echo "<BR>查詢失敗!";     
  19. }//end if()    

 

從上面可以看出不同的函數輸出的格式也是不一樣的,mysqli_fetch_row()返回的是以數字做索引的,mysqli_fetch_assos()是以關鍵字做索引的,而mysqli_fetch_array()和fetch_array()即使用數字也使用關鍵字做索引。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM