Maison  >  Article  >  développement back-end  >  Comment PHP exploite-t-il MySQL pour obtenir des résultats sélectionnés ?

Comment PHP exploite-t-il MySQL pour obtenir des résultats sélectionnés ?

墨辰丷
墨辰丷original
2018-05-17 14:05:331598parcourir

Cet article présente principalement plusieurs méthodes permettant à PHP d'utiliser MySQL afin d'obtenir des résultats sélectionnés. Les amis qui en ont besoin peuvent s'y référer

Si vous utilisez MYSQL_BOTH, vous obtiendrez un tableau contenant à la fois une association et un index numérique. .

Utilisez MYSQL_ASSOC pour obtenir uniquement des index relationnels (tout comme mysql_fetch_assoc()),

Utilisez MYSQL_NUM pour obtenir uniquement des index numériques (tout comme 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); Obtenir le tableau

[@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) Obtenir le tableau

[@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]#

Ce qui suit est un supplément :

Plusieurs méthodes pour obtenir des ensembles de résultats en php

<?php 
 $conn=mysql_connect("localhost","root",""); 
 $select=mysql_select_db("books",$conn); 
  $query="insert into computers(name,price,publish_data) "; 
 $query.="values(&#39;JSP&#39;,28.00,&#39;2008-11-1&#39;)"; 
 $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[&#39;price&#39;].&#39;::&#39;.$row[&#39;publish_data&#39;].”;
} //$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>";  
 } 
?>

Recommandations associées :

Utilisation de l'analyse de cas dans la liste déroulante selectpicker

Html Select Utiliser l'attribut sélectionné pour définir la sélection par défaut

À propos de l'analyse de l'instruction SQL SELECT DISTINCT

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn