Home  >  Article  >  Backend Development  >  In-depth understanding of using mysql_fetch_row() to return query results in the form of an array_PHP tutorial

In-depth understanding of using mysql_fetch_row() to return query results in the form of an array_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:09:061243browse

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

Copy code The code is as follows:

array mysql_fetch_row(int result)

Parameter description is as follows.
result:
The result identifier returned by the function mysql_query() or mysql_db_query() is used to specify the SQL statement type of the data to be obtained.
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 ranges from 0 to the number of record attributes −1, the i-th in the array The element value is the value on the i-th attribute of the record.
Failure: false.
The following mysql_fetch_row() example function is the same as the example in 5.5.1.
Copy code The code is as follows:


2                                                                                                       ");
5 mysql_select_db("Books", $connection);
6 //Query data
7 $query="SELECT * FROM Computers ";
8 $query.="WHERE price >= 20";
9 //echo $query."
";
10 $result=mysql_query($query,$connection);
11 //Use mysql_fetch_row() to obtain data, and output
12 while($row=mysql_fetch_row($result))
13 {
14 echo "Book title: ".$row[1]."
";
15 echo "Price:" $ row [2]. "& Lt; br & gt;";
16 echo "Publishing date:". $ Row [3]. "& Lt; br & gt;";
17 echo "
";
18 }
19 ?>


mysql_fetch_row() gets the data information of the current row. After being referenced, it automatically slides to the next row. In this example, the reference to it in line 12 is:


Copy the code The 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 to the next row; after fetching the last row , the function will return false and the loop ends. In this way, all the data in the result set can be taken out row by row and displayed.


Note
The subscripts in the result array returned by mysql_fetch_row() correspond to the values ​​​​on different attributes, and can only be used in subscripts, not in attribute names. Obtaining the value of the attribute can easily cause confusion in practical applications, so it must be used carefully. At the same time, care should be taken not to use out-of-bounds subscripts during use.
The results of the example running correctly are as follows.


Copy code The code is as follows:
Book title: Data Structure
Price: 20
Publication date: 2001 -01-01
Book title: C language
Price: 23
Publication date: 1998-04-04
Book title: PHP entry technology
Price: 22
Publication date: 2005-05-01



http://www.bkjia.com/PHPjc/327332.html

truehttp: //www.bkjia.com/PHPjc/327332.htmlTechArticleSame as mysql_result(), mysql_fetch_row() can also be used to obtain the query result set. The difference lies in the return of the function. The value is not a string, but an array. The function is defined as follows. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn