PHP Mysql_fetch series of functions

Source: Internet
Author: User
Tags ming php database
This paper mainly introduces the common usage of MySQL mysql_fetch function in PHP, and mentions the use of its lower Fetch_array and Mysql_fetch_row and Mysql_fetch_object functions. We hope to help you.

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:


Array mysql_fetch_array (resource result [, int result_type])


Parameter description:

    • Result: The dataset resource returned by a query function (such as mysql_query)

    • Result_type: Optional constant, indicating the array result type, acceptable values are as follows:

    • Mysql_both: By default, you get an array that contains both associative and numeric indexes, with the field name as the key name

    • MYSQL_ASSOC: Get an array of associated indexes only

    • 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" userid: ". $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
The field masterpiece returned by this function is case-sensitive for an array key value
Using Mysql_fetch_array () is not significantly slower than using mysql_fetch_row (), and also provides significantly more values
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
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
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


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:


Array mysql_fetch_row (resource result)


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:


Object Mysql_fetch_object (Resource result)


Example:


<?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_object ($result)) {echo" userid: ". $row->uid." <br/> "; echo "User name:" $row->username. " <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 recommendations:

PHP database classes for MySQL SQL service

PHP Database Operation Mysqli

Ultra-Lightweight PHP Database Toolkit

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.