如果用了 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>"; } ?>
相关推荐:
使用selectpicker下拉框案例分析
html select 使用selected属性设置默认选择项
关于sql select distinct 语句的解析
以上就是php如何操作mysql获取select 结果的详细内容。