The difference and use of mysql_fetch_row and mysql_fetch_array () in PHP

Source: Internet
Author: User

mysql_fetch_row () gets a row of data from the result set associated with the specified result identity and returns it as an array. Each result column is stored in an array of cells, with an offset starting at 0.

Calling mysql_fetch_row () in turn returns the next row in the result set, or returns FALSE if there are no more rows.

1<?PHP2 $result=mysql_query("Select Id,email from people WHERE id = ' 42 '");3 if(!$result) {4     Echo' Could not run query: '.Mysql_error();5     Exit;6 }7 $row=Mysql_fetch_row($result);8 9 Echo $row[0];// theTen Echo $row[1];//The email value One?>

mysql_fetch_array () is an extended version of Mysql_fetch_row (). In addition to storing data as a numeric index in an array, you can store the data as an associated index, using the field name as the key name.

The second parameter that is optional in mysql_fetch_array () result_type is a constant that can accept the following values: Mysql_assoc,mysql_num and Mysql_both. This feature is for PHP 3.0.7 New Plus. The default value for this parameter is Mysql_both.

If you use Mysql_both, you will get an array that contains both associative and numeric indexes. With MYSQL_ASSOC, only the associated index (as in Mysql_fetch_assoc ()) is used to get the numeric index (as in Mysql_fetch_row () Mysql_num .

<?php    mysql_connect ("localhost", "Mysql_user", "Mysql_password") or Die        ("Could Not connect:". Mysql_error ());    mysql_select_db ("MyDB");    $result = mysql_query ("Select ID, name from mytable");    while ($row = Mysql_fetch_array ($result, Mysql_assoc)) {        printf ("ID:%s  Name:%s", $row ["id"], $row ["Name"]); c9/>}    

  

The difference and use of mysql_fetch_row and mysql_fetch_array () in PHP

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.