In-depth understanding returns query results as arrays with mysql_fetch_row ()

Source: Internet
Author: User

as with mysql_result (), mysql_fetch_row () can also be used to get a query result set, except that the return value of the function is not a string but an array. the function is defined as follows.

Copy CodeThe code is as follows:
Array mysql_fetch_row (int result)


The parameters are described below.
Result:
The result identifier returned by the function mysql_query () or Mysql_db_query (), which specifies the type of SQL statement to get the data.
The function return value is as follows.
success: An array that contains the current row data information in the query result set, the array subscript range 0~ The number of attributes −1, and the value of the I element in the array is the value on the first attribute of the record.
failed:false.
The following mysql_fetch_row () uses the sample feature with the example in 5.5.1.

Copy CodeThe code is as follows:
1 <!------use Mysql_fetch_row () to get data: mysql_fetch_row.php------>
2 <?php
3//Connect and select to database server
4 $connection = mysql_connect ("localhost", "root", "password");
5 mysql_select_db ("Books", $connection);
6//Query data
7 $query = "SELECT * from Computers";
8 $query. = "WHERE price >= 20";
9//echo $query. " <br> ";
$result =mysql_query ($query, $connection);
11//Use Mysql_fetch_row () to obtain data and output
while ($row =mysql_fetch_row ($result))
13 {
echo "title:" $row [1]. " <br> ";
echo "Price:". $row [2]. " <br> ";
echo "Publication date:" $row [3]. " <br> ";
echo "<br>";
18}
?>


Mysql_fetch_row () Gets the data information for the current row and, after being referenced, automatically slides to the next line. In this case, the reference to it in line 12th is:

Copy CodeThe code is as follows:
while ($row =mysql_fetch_row ($result))


In this loop, each time mysql_fetch_row () obtains the current row data, assigns it to the array $row, and then automatically slides down one line; After the last line is fetched, the function returns false and the loop ends. This allows all the data in the result set to be fetched and displayed row by line.
Attention
Mysql_fetch_row () returns the index of the result array corresponding to the values on different attributes, and can only be obtained by subscript, not by using the property name method to obtain the value of the property, in the actual application is easy to cause confusion, must be carefully used. At the same time, it should be noted that in use do not use out of bounds subscript.
The results of the example running correctly are as follows.

Copy CodeThe code is as follows:
Title: Data structure
Price: 20
Publication date: 2001-01-01
Title: C language
Price: 23
Publication date: 1998-04-04
Title: Php Getting Started technology
Price: 22
Publication date: 2005-05-01

In-depth understanding returns query results as arrays with mysql_fetch_row ()

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.