PHP4 and MySQL database operation function detailed explanation (iii)

Source: Internet
Author: User

<5&gt. Database record operation function (5):

1, Mysql_fetch_array ()
Format: array mysql_fetch_array (int query);

Successful execution returns an array that holds the value of the next record, and returns a value of False if execution fails.
The returned array can be represented either by subscript or by field name.

Example:
<?php
$query = mysql_query ($sql, $connect);
while ($arrary = Mysql_fetch_array ($query))
{

echo $array [Column1]. "| ". $array [Column2];
echo $array [0]. "| ". $array [1];

}
?>
Note: The subscript for the array starts at 0!

2, Mysql_fetch_row ()
Format: array = mysql_fetch_row (int query);

The function of the mysql_fetch_array () function is basically the same as 1. The difference is that mysql_fetch_row () can only be represented as an array subscript.
A successful return of an array failed to return a value of false.

Example:
<?php
$query = mysql_query ($sql, $connect);
while ($row = Mysql_fetch_row ($query))
{
echo $row [0]. " | " . $row [1]. "<br>";
}
?>
The Note:mysql_fetch_row () function can only be represented by an array subscript, starting at 0.
Another: Mysql_fetch_row () performs faster than Mysql_fetch_array () and reads the next row of data.

3, Mysql_result ()
Format: int mysql_result (int query, int row, string filedname);

In Mysql_result (), the argument row must start at 0, and the parameter filedname must be a real field name and cannot be represented by a subscript.
Successful execution returns the value of the field retrieved from the database, and returns a false values if it fails.

Example:
<?php
$query = mysql_query ($sql, $connect);
Echo mysql_result ($query, 0, "Column1"). " <br> ";
Echo mysql_result ($query, 1, "Column1"). " <br> ";
Echo mysql_result ($query, 2, "Column1"). " <br> ";
?>

Note: This function is less functional but easy to use.

4, Mysql_fetch_object ()
Format: Object mysql_fetch_object (int query)

Can iterate over a specified field, perform successfully, return a value as Object objects, and return a value of false.

Example:
<?php
$query = mysql_query ($sql, $connect);
while ($object = Mysql_fetch_object ($query))
{
Echo $object->column1. "<br>";
Echo $object->column2. "<br>";
Echo $object->column3. "<br>";
}
?>


The Note:mysql_fetch_object () function returns 1 objects after successful execution.
The operation is as follows:
$object-> Field Name

5, Mysql_data_seek ()
Format: int mysql_data_seek (int row, int query);
Move the cursor to the specified row (row_number)
Execution succeeded, returned a true value, failed, and returned false.
This function can be used in conjunction with mysql_fetch_array () or mysql_fetch_row (), which means that after using the Mysql_data_seek () function, you can use the mysql_fetch_array () or Mysql_fetch_ Row () function to display the specified line.

Example:
<?php
$query = mysql_query ($sql, $connect);
$seek = Mysql_data_seek ($query, 2);
$arrary = Mysql_fetch_array ($query);
echo $array [Column1]. <br> ";
echo $array [Column2]. <br> ";
?>




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.