Four ways to return query data set in PHP _php tutorial

Source: Internet
Author: User
Tags mysql tutorial
A detailed description of the four functions of Mysql_result mysql_fetch_row mysql_fetch_array mysql_fetch_object in PHP

MySQL tutorial _result (): The advantage is ease of use, the disadvantage is that the function is small, one call can only get a row of elements in the result data set, the larger database tutorial less efficient;

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

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

Grammar
Mysql_result (Data,row,field) parameter description
Data required. Specifies the result identifier to use. The identifier is returned by the mysql_query () function.
Row required. Specifies the line number. The line number starts at 0.
field is optional. Specifies which field to get. Can be a field offset value, field name, or Table.fieldname.

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

$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 advantage is that the execution efficiency is the highest in 4 methods, and the disadvantage is that the attribute value can only be obtained by using the number as the attribute index, which is very easy to be confused when used;

The Mysql_fetch_row () function takes a row from the result set as a numeric array.

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

Description
Mysql_fetch_row () Gets a row of data from the result set associated with the result identity data and returns it as an array. Each result column is stored in an array of cells, with an offset starting at 0.

Calling Mysql_fetch_row () in turn returns the next row in the result set, or False if there are no more rows.

return value
Returns an array based on the resulting row, or false if there are no more rows.
Example

$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 the 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 equally high, same as mysql_fetch_row (), and the boundary can be directly obtained attribute value by the attribute name method, so it is most commonly used in practical application.

Definition and usage
The Mysql_fetch_array () function takes a row from the result set as an associative array, or as a numeric array, or both

Returns an array based on the rows obtained from the result set, or False if there are no more rows.

Grammar
Mysql_fetch_array (data,array_type) parameter description
Data is optional. Specifies the data pointers to be used. The data pointer is the result of the mysql_query () function.
Array_type is optional. Specifies what kind of results to return. Possible values:

MYSQL_ASSOC-Associative arrays
Mysql_num-numeric array
Mysql_both-Default. Simultaneous generation of associative and numeric arrays

Hints and Notes
Note: mysql_fetch_array () is an extended version of Mysql_fetch_row (). In addition to storing data as a numeric index in an array, you can store the data as an associated index, using the field name as the key name.

Tip: It is important to point out that using mysql_fetch_array () is not significantly slower than using mysql_fetch_row (), and it clearly provides more values.

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

$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 the person where Lastname= ' Adams '";
$result = mysql_query ($sql, $con);
Print_r (Mysql_fetch_array ($result));

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

Array
(
[0] = = Adams
[LastName] = Adams
[1] = John
[FirstName] = John
[2] = London
[City] = London
)

Mysql_fetch_object (): The use of object-oriented thinking, in the design of ideas more advanced, if used to use object-oriented thinking to write programs, it will be very self-selection. Second, the advantages of this method are also reflected in the more responsible for the structure of the data results, logically clearer.

The Mysql_fetch_object () function takes a row from the result set (Recordset) as an object.

If successful, this function obtains a row from mysql_query () and returns an object. Returns False if there is a failure or no more rows.

Grammar
Mysql_fetch_object (data) parameter description
Data required. The data pointer to use. The data pointer is the result returned from mysql_query ().
Hints and Notes
Note: Each subsequent call to Mysql_fetch_object () will return the next row in the recordset.

Note: mysql_fetch_object () is similar to Mysql_fetch_array (), with only a little difference-the object is returned instead of an array. Indirectly, it also means that the array can only be accessed by the word by, not the offset.
Example

$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. "
";
}

Mysql_close ($con);
?> output:

John
George
Thomas

http://www.bkjia.com/PHPjc/631349.html www.bkjia.com true http://www.bkjia.com/PHPjc/631349.html techarticle php mysql_result mysql_fetch_row mysql_fetch_array mysql_fetch_object Four functions of the detailed description of the MySQL tutorial _result (): The advantages of easy to use , the disadvantage is that the function is less, one time ...

  • 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.