Back to the query data set in php four ways

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags array connect data example function get mysql mysql tutorial

_result (): The advantage is easy to use; The disadvantage is that the function is less, a call can only get a row of elements of the result data set, the efficiency of the larger database tutorial is less;

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. This identifier is returned by the mysql_query () function.
row required. Specify the line number. Line number starting from 0.
field optional. Which field is required? 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 line.

<? 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 advantage of the highest efficiency in the implementation of the four methods; the downside is that only digital as a property index to obtain the value of the property, the use of very prone to confusion;

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 () takes a row of data from the result set associated with the result ID data and returns it as an array. The columns for each result are stored in cells of an array 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 the array generated from the retrieved rows, or false if there are no more rows.
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 same high efficiency, with mysql_fetch_row () is almost the same, and the world can use the property name directly get the property value, so the most commonly used in practical applications;

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

Returns an array generated from 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 rules specify the data pointer to use. The data pointer is the result of the mysql_query () function.
optional array_type What kind of result should be returned? Possible values:

mysql_assoc - associative array
mysql_num - number array
mysql_both - the default. Generate associative and numeric arrays

Tips and notes Note: mysql_fetch_array () is an extended version of mysql_fetch_row (). In addition to storing the data digitally in an array, you can store the data as an associative index with 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 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 ('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 (): the use of object-oriented thinking, more advanced in the design ideas, if accustomed to using object-oriented ideas to write programs, it will be very choose it. Secondly, the advantages of this method are also manifested in that the data structure that is more responsible for the structure is logically clearer.

The mysql_fetch_object () function takes a row from the result set (recordset) as the object.

If successful, this function gets a line from mysql_query () and returns an object. If it fails or there are no more rows, it returns false.

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 () except that it returns objects instead of arrays. Indirectly, this also means that the array can only be accessed by the field name, not 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.