Home >Backend Development >PHP Problem >How to return query data set in php

How to return query data set in php

coldplay.xixi
coldplay.xixiOriginal
2020-11-13 13:41:084324browse

The method of returning the query data set in PHP: 1. Use the function [mysql_result()] to query; 2. Use the function [mysql_fetch_row()] to query; 3. Use the function [mysql_fetch_array()] to query.

How to return query data set in php

Method to return query data set in php:

1, mysql_result() : The advantage is that it is easy to use; the disadvantage is that it has few functions. One call can only obtain one row of elements in the result data set, which is less efficient for larger database tutorials;

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.

Syntax:mysql_result(data,row,field)

Parameters: data required, specifies the result identifier to be used, which is the mysql_query() function return of.

Parameter: row is required, specifies the row number, and the row number starts from 0.

Parameter: field optional, specifies which field to obtain, which can be 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
$con = mysql_connect("localhost", "hello", "321");
if (!$con) {
    die(&#39;could not connect: &#39; . 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);
?>

2, 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. It's very easy to get confused;

mysql_fetch_row() function gets a row from the result set as an array of numbers.

Syntax:mysql_fetch_row(data)

Parameters: data Required, the data pointer to be used, which is the result returned from mysql_query().

Description: mysql_fetch_row() obtains 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 an array unit, and the offset starts from 0.

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

Return value: Return the array generated based on the obtained rows. If there are no more rows, return false. The example is as follows:

<?php
$con = mysql_connect("localhost", "hello", "321");
if (!$con) {
    die(&#39;could not connect: &#39; . mysql_error());
}
$db_selected = mysql_select_db("test_db", $con);
$sql = "select * from person where lastname=&#39;adams&#39;";
$result = mysql_query($sql, $con);
print_r(mysql_fetch_row($result));
mysql_close($con);
?>

Output:

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

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

mysql_fetch_array() function fetches a row from the result set as an associative array, a numeric array, or both, and returns an array generated based on the row fetched from the result set, if there are no more line returns false.

Syntax:mysql_fetch_array(data,array_type)

Parameters: data optional, specifies the data pointer to be used, the data pointer is generated by the mysql_query() function result.

array_type optional, specifies what kind of result is returned, possible values:

  • mysql_assoc - associative array

  • mysql_num - Numeric array

  • mysql_both - By default, both association and numeric arrays are generated

Tips and comments

Comments: 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 obviously provides more values.

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

<?php
$con = mysql_connect("localhost", "hello", "321");
if (!$con) {
    die(&#39;could not connect: &#39; . mysql_error());
}
$db_selected = mysql_select_db("test_db", $con);
$sql = "select * from person where lastname=&#39;adams&#39;";
$result = mysql_query($sql, $con);
print_r(mysql_fetch_array($result));
mysql_close($con);
?>
//输出类似: 
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 If you write a program with an object-oriented approach, you will feel free to choose it. Secondly, the advantage of this method is that the data results with a more responsible structure are logically clearer.

4, mysql_fetch_object() The function obtains 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. If it fails or there are no more rows, it returns false.

Syntax:mysql_fetch_object(data)

Parameters: data Required, the data pointer to be used, which 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 - it returns an object instead of an array, which indirectly means that the array can only be accessed through the field name, not the offset. Example:

<?php
$con = mysql_connect("localhost", "peter", "abc123");
if (!$con) {
    die(&#39;could not connect: &#39; . 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 free learning recommendations: php programming (video)

The above is the detailed content of How to return query data set in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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