Tutorial on common usage of MySQL mysql_fetch _ * function in PHP, and tutorial on _ PHP

Source: Internet
Author: User
A common usage tutorial on MySQL mysql_fetch _ * functions in PHP ,. Tutorial on common usage of MySQL mysql_fetch _ * function in PHP, mysql_fetch _ * The main function of the column function mysql_fetch _ * is to obtain the relevant query results from the returned results of the query. a common usage tutorial on using PHP to operate the mysql_fetch _ * function of MySQL is provided,

Mysql_fetch _ * column function
The main function of the mysql_fetch _ * column function is to obtain relevant query results from the returned results of the query, including:

  • Mysql_fetch_array (): Gets a row from the result set as an associated array or index array, or both
  • Mysql_fetch_row (): obtains a row from the result set as an enumeration array.
  • Mysql_fetch_assoc (): obtains a row from the result set as the associated array.
  • Mysql_fetch_object (): obtains a row from the result set as an object.
  • Mysql_fetch_field (): obtains field information from the result set and returns it as an object.
  • Mysql_fetch_lengths (): Gets the output length of each field in a row in the result set.
  • Mysql_fetch_array ()

The mysql_fetch_array () function is used to obtain a row from the result set as an associated array or index array, or both. An array is returned. otherwise, FALSE is returned.

Syntax:

array mysql_fetch_array( resource result [, int result_type] )

Parameter description:

  • Result: query the dataset resources returned by a function (such as mysql_query ).
  • Result_type: an optional constant indicating the type of the array result. the acceptable values are as follows:
  • MYSQL_BOTH: by default, an array containing both correlated and digital indexes is obtained. field names are used as key names.
  • MYSQL_ASSOC: obtains only the associated index array.
  • MYSQL_NUM: returns an array of numeric indexes only.

Example 1: Use MYSQL_NUM:

<? Php $ conn = @ mysql_connect ("localhost", "root", "root123"); if (! $ Conn) {die ("failed to connect to the database :". mysql_error ();} mysql_select_db ("test", $ conn); mysql_query ("set character set 'gbk'"); $ result = mysql_query ("SELECT uid, username FROM user "); while ($ row = mysql_fetch_array ($ result, MYSQL_NUM) {echo" user ID :". $ row [0]."
"; Echo" username: ". $ row [1]."
";}?>

Browser output:

User ID: 1 user name: admin User ID: 2 user name: James User ID: 3 User Name: Jack User ID: 4 user name: John

Example 2: use MYSQL _ ASSOC:

// Duplicate code omitting $ result = mysql_query ("SELECT uid, username FROM user"); while ($ row = mysql_fetch_array ($ result, MYSQL _ ASSOC) {echo "user ID: ". $ row ['uid']."
"; Echo" username: ". $ row ['username']."
";}

The output content of the browser is the same as that of the browser.
If MYSQL_BOTH is used or this parameter is omitted, MYSQL_NUM and MYSQL _ ASSOC are also available.
Description
The field names returned by this function are case-sensitive as array key values.
Mysql_fetch_array () is not significantly slower than mysql_fetch_row (), and more values are provided.
This function only retrieves a row of data from the current data pointer and returns the result. if it is executed once, the data pointer is directed to the next column of data.
If you want to retrieve multiple or all rows of data, you need to use a circular structure to retrieve the data row by row.
If two or more columns in the result have the same field name, the last column takes precedence. To access other columns with the same name, you must use the numeric index of the column or create an alias for the column.


Mysql_fetch_row ()
The MySQL operation function mysql_fetch_row () in PHP is used to retrieve a row from the result set as an enumeration array. An array is returned. otherwise, FALSE is returned.
Syntax:

array mysql_fetch_row( resource result )

This function is consistent with mysql_fetch_array (resource result, MYSQL_NUM). For more information, see the usage of mysql_fetch_array.

Mysql_fetch_object ()
The MySQL function mysql_fetch_object () in PHP is used to obtain a row from the result set as an object. an object is returned successfully; otherwise, FALSE is returned.
Syntax:

object mysql_fetch_object( resource result )

Example:

<? Php $ conn = @ mysql_connect ("localhost", "root", "root123"); if (! $ Conn) {die ("failed to connect to the database :". mysql_error ();} mysql_select_db ("test", $ conn); mysql_query ("set character set 'gbk'"); $ result = mysql_query ("SELECT uid, username FROM user "); while ($ row = mysql_fetch_object ($ result) {echo" user ID :". $ row-> uid."
"; Echo" username: ". $ row-> username ."
";}?>

Browser output:

User ID: 1 user name: admin User ID: 2 user name: James User ID: 3 User Name: Jack User ID: 4 user name: John

Articles you may be interested in:
  • A simple PHP & MYSQL message board source code
  • Summary MySQL Chinese garbled characters, phpmyadmin garbled characters, causes of php garbled characters and solutions
  • JQuery uses PHP + MySQL to implement a list of two-level linkage drop-down lists [instances]

For a common usage tutorial of the aggregate function, the main function of the mysql_fetch _ * column function is to obtain the relevant query results from the returned results of the query...

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.