Four methods to return query data set in php_PHP tutorial

WBOY
Release: 2016-07-13 16:59:02
Original
1362 people have browsed it

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 that it is easy to use; its disadvantage is that it has few functions, and one call can only obtain one row of elements in the result data set, which is relatively Large database tutorials are 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 failed, returns false.

Grammar
mysql_result(data,row,field) parameter description
data required. Specifies the result identifier to use. This identifier is returned by the mysql_query() function.
row required. Specify the line number. Line numbers start from 0.
field is optional. Specifies which field to retrieve. Can be a field offset value, field name or table.fieldname.

If this parameter is not specified, this 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 among the four methods; the disadvantage is that only numbers can be used as attribute indexes to obtain attribute values, which is very easy to cause confusion when using;

The mysql_fetch_row() function fetches 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() fetches a row of data from the result set associated with the result identifier data and returns it as an array. Each result column is stored in a cell of the array, starting at offset 0.

Sequential calls to mysql_fetch_row() will return the next row in the result set, or false if there are no more rows.

Return value
Returns an array based on the rows fetched, 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 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, almost the same as mysql_fetch_row(), and the attribute value can be obtained directly using the attribute name, so it is most commonly used in practical applications;

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

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

Grammar
mysql_fetch_array(data,array_type) parameter description
data optional. The specification specifies the data pointer to be used. This data pointer is the result of the mysql_query() function.
array_type optional. Specifies what kind of results are returned. Possible values:

mysql_assoc - associative array
mysql_num - numeric array
mysql_both - default. Produce both associative and numeric arrays

Tips and Notes
Note: mysql_fetch_array() is an extended version of mysql_fetch_row(). In addition to storing data in an array as a numerical index, you can also store data as an associative index, using the field name as the key.

Tip: It is important to point out that using mysql_fetch_array() is not significantly slower than using mysql_fetch_row(), and it also provides significantly 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 person where lastname='adams'";
$result = mysql_query($sql,$con);
print_r(mysql_fetch_array($result));

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

array
(
[0] => adams
[lastname] => adams
[1] => john
[firstname] => john
[2] => london
[city] => london
)

mysql_fetch_object(): It adopts object-oriented thinking and is more advanced in design ideas. If you are used to writing programs with object-oriented ideas, you will choose it easily. Secondly, the advantage of this method is that the data results with a more responsible structure are logically clearer.

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

If successful, this function gets a row from mysql_query() and returns an object. Returns false on failure or if there are 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().
Tips and Notes
Note: Each subsequent call to mysql_fetch_object() returns the next row in the recordset.

Note: mysql_fetch_object() is similar to mysql_fetch_array(), with one difference - an object is returned instead of an array. Indirectly, it also means that the array can only be accessed by field name, not 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631349.htmlTechArticleMysql_result mysql_fetch_row mysql_fetch_array mysql_fetch_object Detailed description of the four functions in php mysql tutorial_result(): The advantage is that it is easy to use; Its disadvantage is that it has few functions and once...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template