PHP4 and MySQL database operation functions (2) _ MySQL

Source: Internet
Author: User
5. database Record operation functions (5): 1. mysql_fetch_array () format: arraymysql_fetch_array (intquery); if execution is successful, an array is returned, which stores the value of the next record, if the execution fails, False is returned. The returned array can be expressed by subscript or field name. Example: $ querymysql_qu <5>. database record operation functions (5 ):

1. mysql_fetch_array ()
Format: array mysql_fetch_array (int query );

If the execution is successful, an array is returned. the array contains the value of the next record. if the execution fails, False is returned.
The returned array can be expressed by subscript or field name.

Example:
$ Query = mysql_query ($ SQL, $ connect );
While ($ arrary = mysql_fetch_array ($ query ))
{

Echo $ array [column1]. "|". $ array [column2];
// Echo $ array [0]. "|". $ array [1];

}
?>
Note: the subscript of the array starts from 0!

2. mysql_fetch_row ()
Format: array = mysql_fetch_row (int query );

The function is basically the same as the mysql_fetch_array () function of 1. The difference is that mysql_fetch_row () can only be expressed as an array subscript.
1 array is returned successfully, and False is returned if the operation fails.

Example:
$ Query = mysql_query ($ SQL, $ connect );
While ($ row = mysql_fetch_row ($ query ))
{
Echo $ row [0]. "|". $ row [1]."
";
}
?>
Note: The mysql_fetch_row () function can only be represented by an array subscript and start from 0.
In addition, mysql_fetch_row () is faster than mysql_fetch_array () and reads data from the next row.

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

In mysql_result (), the row parameter must start from 0, and the filedname parameter must be a real field name, which cannot be expressed by subscript.
If the execution is successful, the value of the field retrieved from the database is returned. if the execution fails, the value of False is returned.

Example:
$ Query = mysql_query ($ SQL, $ connect );
Echo mysql_result ($ query, 0, "column1 ")."
";
Echo mysql_result ($ query, 1, "column1 ")."
";
Echo mysql_result ($ query, 2, "column1 ")."
";
?>

Note: This function has few functions but is easy to use.

4. mysql_fetch_object ()
Format: object mysql_fetch_object (int query)

The specified field can be read cyclically. if the execution is successful, a value is returned in the form of an object. if the operation fails, a value of False is returned.

Example:
$ Query = mysql_query ($ SQL, $ connect );
While ($ object = mysql_fetch_object ($ query ))
{
Echo $ object-> column1 ."
";
Echo $ object-> column2 ."
";
Echo $ object-> column3 ."
";
}
?>


Note: After the mysql_fetch_object () function is successfully executed, one object is returned!
The procedure 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)
If the execution is successful, true value is returned. if the execution fails, False is returned.
This function can be used with mysql_fetch_array () or mysql_fetch_row (). after using the mysql_data_seek () function, you can use mysql_fetch_array () or mysql_fetch_row () function to display the specified row.

Example:
$ Query = mysql_query ($ SQL, $ connect );
$ Seek = mysql_data_seek ($ query, 2 );
$ Arrary = mysql_fetch_array ($ query );
Echo $ array [column1]."
";
Echo $ array [column2]."
";
?>

<6> database-level database operation functions (2 ):

1. mysql_create_db ()
Format: int mysql_create_db (string database name, int link_identifier );

You can use the mysql_query () or mysql_db_query () function to create or delete a database.

However, we can use this function to create a database more conveniently.
If one true value is returned successfully, if one fails, one false is returned.

Example:


$ Connect = mysql_connect ("$ hostname", "$ user", "$ pwd ");
$ Create = mysql_create_db ("dbtest", $ connect );
If ($ create) echo "create database dbtest successed! ";
Else echo "create database dbtest failed! ";

?>


2. mysql_drop_db ()
Format: int mysql_drop_db (string database name, int link_identifier );

You can use a program to delete one database.

However, we can use this function to delete one database more conveniently.
If one true value is returned successfully, if one fails, one false is returned.

Example:


$ Connect = mysql_connect ("$ hostname", "$ user", "$ pwd ");
$ Create = mysql_drop_db ("dbtest", $ connect );
If ($ create) echo "drop datebase dbtest successed! ";
Else echo "drop datebase dbtest failed! ";

?>

Note: If mysql_query () or mysql_db_query () is used, the SQL statement should be:
(1) create database dbtest
(2) drop datebase dbtest

Reprint: www.chinaphp.com

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.