How PHP operates MySQL get select results

Source: Internet
Author: User
This article mainly introduces the PHP operation MySQL to obtain the select results of several methods, the need for friends can refer to the following

If you use Mysql_both, you will get an array that contains both associative and numeric indexes.

Use MYSQL_ASSOC only to get the associated index (as in Mysql_fetch_assoc ()),

Use Mysql_num only to get a numeric index (as in 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 "Not connected successfully!";  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); Get array

[@test01 model]# more test.php <?php  $link =mysql_connect ("10.12.136.181", "Hello", "Hello");  if (! $link)   echo "Not connected successfully!";  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] = Hellokitt Y  [name] = Hellokitty  [1] = i dont kno  [addr] = i dont kno) [@test01 model]#

3.mysql_fetch_array ($rs, mysql_num) get array

[@test01 model]# php test.php Array (  [0] = Hellokitty  [1] = i dont kno) [@test01 model]# more test.php & lt;? PHP  $link =mysql_connect ("10.12.136.181", "Hello", "Hello");  if (! $link)   echo "Not connected successfully!";  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]#

Here are the additions:

Several methods of getting the result set by 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);  The following is the use of the mysql_result () function to obtain a query result $num =mysql_num_rows ($result);  for ($rows _count=0; $rows _count< $num; $rows _count++) {echo "title:". Mysql_result ($result, $rows _count, "name");  echo "Price:". Mysql_result ($result, $rows _count, "prices"); echo "Published Date:". Mysql_result ($result, $rows _count, "Publish_data"). "  <br> "; }//The following is the use of the mysql_fetch_row () function to get to the query result while ($row =mysql_fetch_row ($result)) {echo "ISBN:". $row [0]. "  <br> "; echo "title:" $row [1]. "  <br> "; echo "Price:". $row [2]. "  <br> "; echo "Publication date:". $row [3]. "  <br> ";  echo "<br>"; }//The following is the use of the mysql_fetch_array () function to get to the query result while ($row =mysql_fetch_array ($result)) {echo "ISBN:". $row [0]. "  <br> "; echo "title:" $row [1]. "  <br> "; echo "Price:". $row ["Prices"]. " <br> "; echo "Publication date:". $row ["Publish_data"]. "  <br> ";  echo "<br>"; }//MYSQL_FETCH_ASSOC () Same as mysql_fetch_array ($result, MYSQL_ASSOC) while ($row = Mysql_fetch_assoc ($res)) {echo $row [' Price ']. ':: '. $row [' Publish_data ']. ";} $row [0] cannot take value//The following is the use of the Mysql_fetch_object () function to get to the query result while ($row =mysql_fetch_object ($result)) {echo "ISBN:". $row Id. "  <br> "; echo "title:" $row->name. "  <br> "; echo "Price:" $row->price. "  <br> "; echo "Date of publication:" $row->publish_data. "  <br> ";   echo "<br>"; }?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.