How to get the current row data with mysql_fetch_array () detailed _php tips

Source: Internet
Author: User
Like Mysql_fetch_row (), the function mysql_fetch_array () is also the current row of data in the result set and automatically slides down the line after the call. It is defined as follows.
Copy Code code as follows:

Array mysql_fetch_array (int result, int [result_type])

The parameters are described below.
(1) Result: An identifier returned by the function mysql_query () or mysql_db_query () that specifies the type of SQL statement for the data to be obtained.
(2) Result_type: For specifying a result set type, optionally, a value range of Php constants set {MYSQL_ASSOC, Mysql_num, Mysql_both}.
The function returns the following values.
Success:An array that contains the current row data information in the query result set, the array subscript range 0~ the number of record attributes −1, and the first element in the array is the value on the first property of the record. You can also use the property name to get the value on the property.
Failed: false.
The following example uses Mysql_fetch_array () to get book information that is not less than 20 in the computers datasheet.
Copy Code code as follows:

1 <!----use mysql_fetch_array () to get data: mysql_fetch_array.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);
//mysql_fetch_array () Get the data and output
while ($row =mysql_fetch_array ($result))
13 {
echo "title:". $row [1]. " <br> ";
echo "Prices:". $row [Price]. " <br> ";
echo "Published date:" $row ["Publish_date"]. " <br> ";
echo "<br>";
18}
?>

The example uses Mysql_fetch_array () on line 12th to get the current row data, and then in the loop in line 12th to 18th, use the property index and the property name to get the value on a property of the row. It is not hard to see that the difference between mysql_fetch_array () and Mysql_fetch_row () is that the former returns two backups in the array that hold the result set, one accessible through the property index, and the other through the property name.
Attention
When a value on a property is accessed through a property name, if there are several properties with the same name, then the indexed property overrides the previous property value. Therefore, when you use the property name to get data, avoid the duplicate attribute names in the query result set.
The results of the sample run are as follows.
Copy Code code as follows:

Title: Data structure
Price: 20
Release Date: 2001-01-01
Title: C language
Price: 23
Release Date: 1998-04-04
Book title: Introduction to PHP Technology
Price: 22
Release Date: 2005-05-01

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.