Php returns four methods to query a dataset

Source: Internet
Author: User
Tags mysql tutorial

Detailed descriptions of four functions of mysql_result mysql_fetch_row mysql_fetch_array mysql_fetch_object in php

Mysql tutorial _ result (): The advantage lies in ease of use; its disadvantage is that there are few functions. One call can only obtain one row of elements in the result dataset, which is less efficient for large-scale database tutorials;

The value of a field in the returned result set returned by the mysql_result () function.

If successful, the function returns the field value. If it fails, false is returned.

Syntax
Mysql_result (data, row, field) parameter description
Data is required. Specifies the result identifier to be used. This identifier is returned by the mysql_query () function.
Row is required. Specifies the row number. The row number starts from 0.
Field is optional. Specifies which field to obtain. It can be a field offset value, field name or table. fieldname.

If this parameter is not specified, the function obtains the first field from the specified row.
 

<? Php tutorial
$ Con= mysql_connect ("localhost", "hello", "321 ");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}

$ Db_selected = mysql_select_db ("test_db", $ con );

$ SQL = "select * from person ";
$ Result = mysql_query ($ SQL, $ con );

Echo mysql_result ($ result, 0 );

Mysql_close ($ con );

?>

Mysql_fetch_row (): The execution efficiency is the highest among the four methods. The disadvantage is that you can only use numbers as attribute indexes to obtain attribute values, which are prone to confusion during use;

The mysql_fetch_row () function retrieves a row from the result set as a numeric array.

Syntax
Mysql_fetch_row (data) parameter description
Data is required. The Data Pointer to use. The Data Pointer is the result returned from mysql_query.

Description
Mysql_fetch_row () retrieves a row of data from the result set associated with the result ID data and returns it as an array. Each result column is stored in an array unit, and the offset starts from 0.

Call mysql_fetch_row () in sequence to return the next row in the result set. If no more rows exist, false is returned.

Return Value
Returns the array generated based on the row obtained. If no more rows exist, false is returned.
Example

<? Php
$ Con= mysql_connect ("localhost", "hello", "321 ");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}

$ Db_selected = mysql_select_db ("test_db", $ con );
$ SQL = "select * from person where lastname = 'adams '";
$ Result = mysql_query ($ SQL, $ con );
Print_r (mysql_fetch_row ($ result ));

Mysql_close ($ con );
?> Output:

Array
(
[0] => adams
[1] => john
[2] => london
)


Mysql_fetch_array (): The execution efficiency is the same as that of mysql_fetch_row (), and attribute values can be directly obtained using attribute names. Therefore, this is the most common method in practical applications;

Definition and usage
The mysql_fetch_array () function retrieves a row from the result set as an associated array, or an array of numbers, or both.

Returns the array generated based on the rows obtained from the result set. If no more rows exist, false is returned.

Syntax
Mysql_fetch_array (data, array_type) parameter description
Data is optional. Specifies the Data Pointer to be used. This Data Pointer is produced by the mysql_query () function.
Array_type is optional. Specifies the result to be returned. Possible values:

Mysql_assoc-associated array
Mysql_num-number Array
Mysql_both-default. Generate join and numeric arrays at the same time
 
Tips and comments
Note: mysql_fetch_array () is an extended version of mysql_fetch_row. In addition to storing data in an array as a digital index, you can also store data as an associated index and use the field name as the key name.

Note: It is important to note that mysql_fetch_array () is not significantly slower than mysql_fetch_row (), and more values are obviously provided.

Note: The field names returned by this function are case sensitive.
Example

<? Php
$ Con= mysql_connect ("localhost", "hello", "321 ");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}

$ Db_selected = mysql_select_db ("test_db", $ con );
$ SQL = "select * from person where lastname = 'adams '";
$ Result = mysql_query ($ SQL, $ con );
Print_r (mysql_fetch_array ($ result ));

Mysql_close ($ con );
?> The output is similar:

Array
(
[0] => adams
[Lastname] => adams
[1] => john
[Firstname] => john
[2] => london
[City] => london
)

Mysql_fetch_object (): it adopts the object-oriented idea and is more advanced in design ideas. If you are used to writing programs with object-oriented thinking, you will choose it from your own. Secondly, the advantages of this method are also reflected in the logic clarity of the data results with a more responsible structure.

The mysql_fetch_object () function retrieves a row from the result set (record set) as an object.

If successful, this function obtains a row from mysql_query () and returns an object. If the row fails or no more rows exist, false is returned.

Syntax
Mysql_fetch_object (data) parameter description
Data is required. The Data Pointer to use. The Data Pointer is the result returned from mysql_query.
Tips and comments
Note: Each subsequent call to mysql_fetch_object () will return the next row in the record set.

Note: mysql_fetch_object () is similar to mysql_fetch_array (). There is only one difference-the returned object is not an array. Indirectly, it means that you can only access the array by field name, rather than the offset.
Example

<? Php
$ Con = mysql_connect ("localhost", "peter", "abc123 ");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}

$ Db_selected = mysql_select_db ("test_db", $ con );
$ SQL = "select * from person ";
$ Result = mysql_query ($ SQL, $ con );

While ($ row = mysql_fetch_object ($ result ))
{
Echo $ row-> firstname. "<br/> ";
}

Mysql_close ($ con );
?> Output:

John
George
Thomas

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.