MySQL Fetch series functions

Source: Internet
Author: User
Tags ming

The browser output is as follows.

When you use Mysql_both or omit this argument, you will have both Mysql_num and mysql_ ASSOC attributes.

MySQL the Mysql_fetch_array function obtains a row in the query result as an array

mysql_fetch_* column Functions

The main function of the mysql_fetch_* column function is to obtain related query results from the result set returned by the query, mainly including:

    • Mysql_fetch_array (): Gets a row from the result set as an associative array or an indexed array, or both
    • Mysql_fetch_row (): Gets a row from the result set as an enumeration array
    • MYSQL_FETCH_ASSOC (): Gets a row from the result set as an associative array
    • Mysql_fetch_object (): Gets a row from the result set as an object
    • Mysql_fetch_field (): Gets field information from the result set and returns as an object
    • Mysql_fetch_lengths (): Gets the length of each field content output in the result set

mysql_fetch_array ( )

The Mysql_fetch_array () function is used to get a row from the result set as an associative array or an indexed array, or both. Returns an array successfully, otherwise returns FALSE.

Grammar:

parameter Description:
Parameters Description
Result A dataset resource returned by a query function (such as mysql_query)
Result_type

An optional constant indicating the array result type, acceptable values are as follows:

  1. Mysql_both: By default, you get an array that contains both associative and numeric indexes, with the field name as the key name
  2. MYSQL_ASSOC: Get an array of associated indexes only
  3. Mysql_num: An array that only gets a numeric index

Example 1, using Mysql_num:

<?php $conn = @mysql_connect ("localhost", "root", "root123");
if (! $conn){   
     Die ("Connection Database failed:". 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]. " <br/> ";  
  echo "User name:". $row [1]. " <br/> "; }?>

Browser output:

User Id:1
User name: admin
User Id:2
User name: Xiao Ming
User Id:3
User name: Jack 
User Id:4
User name: Xiao Wang

Example 2, using Mysql_ ASSOC:

Duplicate code omitted $result = mysql_query ("Select Uid,username from user");
while ($row = Mysql_fetch_array ($result, Mysql_ ASSOC)){   
echo "User id:". $row [' uid ']. " <br/> ";   
echo "Username:" $row [' username ']. " <br/> ";
}

The browser output is as follows.

When you use Mysql_both or omit this argument, you will have both Mysql_num and mysql_ ASSOC attributes.

Description
  1. The field masterpiece returned by this function is case-sensitive for an array key value
  2. Using Mysql_fetch_array () is not significantly slower than using mysql_fetch_row (), and also provides significantly more values
  3. This function only takes a row of data from the current data pointer and returns it as a result, and if executed once, points the data pointer to the next column of data
  4. If you want to get multiple rows or all of the data, you need to use a looping structure to take the data out line by row
  5. If two or more columns in the result have the same field name, the last column takes precedence. To access another column of the same name, you must either use the column's numeric index or give the column an alias
  6. mysql_fetch_row ()

    PHP's MySQL operation function mysql_fetch_row () is used to get a row from the result set as an enumerated array. Returns an array successfully, otherwise returns FALSE.

    Grammar:

    This function behaves in accordance with mysql_fetch_array (resource result, mysql_num), please refer to the mysql_fetch_array () function usage, not in this context.

    mysql_fetch_object ()

    PHP Operation MySQL function Mysql_fetch_object () is used to get a row from the result set as an object, return an object successfully, otherwise return FALSE.

    Grammar:

    Example:

    <?php $conn = @mysql_connect ("localhost", "root", "root123"); if (! $conn) {die     ("Connect database failed:". 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. " <br/> ";     

    Browser output:

    User Id:1 User name: Admin user id:2 user name: Xiao Ming user id:3 user name: Jack user id:4 user name: Xiao Wang





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.