Several common database operation functions in MySQL

Source: Internet
Author: User

Some common MYSQL operation functions are closely related to MYSQL operations. I believe they will be of great help to beginners of PHP development.

1. mysql_connect -- open a link to the MySQL server

Note: The link is closed after the page is executed.

Example:

$ Link = mysql_connect ("localhost", "root", "") or die ("cocould not connect:". mysql_error ());

2. mysql_close -- close the MySQL Link

Function Format: bool mysql_close ([resource link_identifier])

Note: link_identifier closes the last non-persistent link by default.

Example:

$ Link = myql_connect ("localhost", "root", "") or die ("link failed! Error message: ". mysql_error ());

Echo "link successful ";

Mysql_close ($ link );?

3. mysql_pconnect -- open an execution link to the MySQL server

Note: similar to mysql_connect, only persistent links are established, which is more common and convenient.

Example:

$ Link = mysql_pconnect ("localhost", "root", "") or die ("cocould not connect:". mysql_error ());

4. mysql_select_db -- select MySQL database

Function Format: bool mysql_select_db (string database_name, resource link_identifier)

Note: after the connection is successful, you should immediately use this function to specify a database for the link identifier.

Example:

$ Link = myql_connect ("localhost", "root", "") or die ("link failed! Error message: ". mysql_error (); echo" link successful ";

Mysql_select_db ("example", $ link) or die ('unable to use example database ');

Mysql_query ("insert into users (name) values ('zhang san ')");

Mysql_close ($ link );

5. mysql_query -- send a MySQL statement

Note: This is the most common function. It is used to execute almost all SQL statements and MySQL-specific commands.

Function Format: resource mysql_query (string query, resource link_identifier)

Example:

$ Result = mysql_query ("SELECT * FROM users ");

6. mysql_num_rows -- get the number of rows in the result set

Function Format: int mysql_rows (resource result)

Description: gets the number of records in the result set, which is only valid for the result set returned by SELECT. For UPDATE/INSERT Results, use the mysql_affected_rows function to obtain the number of affected records.

Example:

$ Result = my example SQL _query ("SELECT * FROM users", $ link );

$ Num_rows = mysql_num_rows ($ result );

Echo "found". $ num_rows. "record \ n ";

7. mysql_affected_rows -- obtain the number of records affected by the previous MySQL operation

Function Format: int mysql_affected_rows (resource link_identifier)

Note: This function should be executed immediately after the corresponding SQL mysql_query function is executed.

Example:

Mysql_query ("delete from users WHERE id <10 ");

Echo "this operation deleted". mysql_affected_rows (). "Row record ";

8. mysql_error -- return the text error message generated by the previous MySQL operation

Function Format: string mysql_error (resource link_identifier)

Description: it returns the text of the last MySQL error.

9. mysql_insert_id -- get the ID generated by the previous INSERT operation

Function Format: int mysql_insert_id (resource link_identifier)

Description: used to obtain the primary key value automatically generated by MySQL during the last INSERT operation.

Example:

Mysql_query ("insert into users (name) values ('kossu ')");

Echo ("id of the last insert operation". mysql_insert_id ());

10. mysql_fetch_array -- get a row from the result set as an associated array, or an array of numbers, or both

Function Format: array mysql_fetch_array (resource result, int result_type)

Note: retrieve a record from the result set in the form of an array and move down a cursor. Through this array, you can access the target data.

Parameter description: result: The result set obtained through the mysql_query function.

Result_type: it can only be MYSQL_ASSOC, MYSQL_NUM, or MYSQL_BOTH, which indicates the array index format is returned. The default value is MYSQL_BOTH.

Example:

$ Result = mysql_query ("SELECT id, name FROM users ");

While ($ row = mysql_fectch_array ($ result ))

{

Echo "id =". $ row [0]. "; name =". $ row ["name"]."

";

}

With reference to these functions, you can write a PHP-based MYSQL operation class, which includes MYSQL connection, link closure, data deletion, modification, and content addition.

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.