How to use mysql_fetch_array () to obtain the data of the current row _ PHP Tutorial

Source: Internet
Author: User
Use mysql_fetch_array () to obtain the data of the current row. Similar to mysql_fetch_row (), the mysql_fetch_array () function is used to obtain the data of the current row in the result set and automatically slide down the row after the call. It is defined as follows. The code for copying the code is as follows: Similar to mysql_fetch_row (), the mysql_fetch_array () function is used to obtain the data of the current row in the result set and automatically slide down the row after the call. It is defined as follows.

The code is as follows:


Array mysql_fetch_array (int result, int [result_type])


The parameters are described as follows.
(1) result: the result ID returned by mysql_query () or mysql_db_query () is used to specify the SQL statement type of the data to be obtained.
(2) result_type: it is used to specify the result set type. optional. the value range is the PHP constant set {MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH }.
The function return value is as follows.
Successful:An array that contains the data of the current row in the query result set. the subscript range of the array is 0 ~ The number of record attributes − 1. the value of the I-th element in the array is the value of the I-th attribute of the record. You can also use the attribute name to obtain the value of this attribute.
Failed: false.
The following example uses mysql_fetch_array () to obtain the books whose price is not less than 20 in the Computers data table.

The code is as follows:


1
2 3 // Connect and select 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 ."
";
10 $ result = mysql_query ($ query, $ connection );
11 // mysql_fetch_array () obtain the data and output
12 while ($ row = mysql_fetch_array ($ result ))
13 {
14 echo "title:". $ row [1]."
";
15 echo "price:". $ row ["price"]."
";
16 echo "publication date:". $ row ["publish_date"]."
";
17 echo"
";
18}
19?>


In the example, use mysql_fetch_array () in row 12th to obtain the data of the current row ~ In the cycle of the 18 rows, the attribute index and attribute name are used to obtain the values of a certain attribute in this row. It is not hard to see that the difference between mysql_fetch_array () and mysql_fetch_row () is that the former returns two copies of the result set in the array, and the other one can be accessed through the attribute index, the other one can be accessed through the attribute name.
Note:
When you access the value of a property by using the property name, if the names of several attributes are the same, the attribute after the index overwrites the previous attribute value. Therefore, when using attribute names to obtain data, avoid duplicate attributes in the query result set.
The running result of the example is as follows.

The code is as follows:


Title: Data structure
Price: 20
Published on: 2001-001
Title: C language
Price: 23
Published on:
Title: PHP entry-level technology
Price: 22
Published on:


Similarly, the function mysql_fetch_array () is used to obtain the data of the current row in the result set and automatically slide down the next row after the call. It is defined as follows. The code is as follows :...

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.