Learn while learning PHP-(16) PHP use MySQL Extension Library to operate the database

Source: Internet
Author: User
Learn while learning PHP-(16) PHP uses the MySQL extension library to operate on the database PHP provides a lot of extension libraries. here we are talking about using the MySQL extension Library, however, such extension libraries will be discarded in the near future, because if the code written in the MySQL extension Library is run, there will be a warning of warning. I originally wanted to write another one directly, but I thought it was the foundation. MySQL extension Library, once it comes to the database, is naturally a bunch of functions, many functions constitute a library, using the extension Library, that is, using the functions in. The MySQL extension Library is fully process-oriented. it is understandable that it does not conform to the object-oriented features and is abandoned. If you don't talk much about it, focus on it directly.

I. PHP uses the MySQL extension library to operate the database

This figure is self-drawn and may be inaccurate.

You can see that using MySQL to operate the database goes through five steps:

1. connect to the MySQL server

The function used here is the mysql_connect () function.

2. select a database

Here mysql_select_db (); function is used

3. execute SQL statements

The mysql_query () function is used here. The so-called SQL statement is the addition, deletion, modification, and query operation.

4. disable the result set.

Here mysql_free_result (); function is used to release system resources.

5. disconnect from the server

Here mysql_close (); function is used

Step 2: connect to the MySQL server

The syntax of the mysql_connect () function is as follows:

Resource mysql_connect ([string server [, string username [, string password [, bool new_link [, int client_flags]);

This is the most complete function parameter, but we can only use the first three parameters when using it.

The first server refers to the MySQL server, which can use the default localhost.

The second username is the user name set during database installation. the default username is root.

The third password is the database password.

If the connection succeeds, a MySQL connection id is returned. if the connection fails, false is returned.

For example, if I connect to my own database, my server is localhost, the user name is root, and the password is root. I can test it like this.

    
2. select a database

Bool mysql_select_db (string database_name [, resource link_identifier]);

The first parameter database_name is the name of the database to be connected.

The second parameter link_identifier is the database connection ID, that is, the value returned after the connection to the MySQL server is successful. If this parameter is left blank, the database connection opened last time is used by default. Recommended.

True is returned for success, and false is returned for failure.

For example, if I need to connect to the test database I created last time, I can write it like this:

     


3. execute SQL statements

The so-called SQL statement is to add, delete, modify, and query the database. We have already introduced the use of command lines to operate databases. The function syntax is as follows:

Resource mysql_query (string query [, resource link_identifier]);

The first parameter is the SQL statement to be executed.

The second parameter is the connection id returned by mysql_connect. Recommended.

SQL statements include ddl [data definition statements], dml [data operation statements, such as update, insert, delete], dql [select], and dtl [data transaction statement rollback commit ], dql statements are the most common query statements. After the dql statement is successfully executed, a "data pointer" is returned. after the dml statement is successfully executed, true is returned. if the dml statement fails, false is returned.

For example, if I want to query the data in my table, I can write it like this.

   ';}

The addition, deletion, and modification of SQL statements are replaced. Then return true or false. You do not need to retrieve the result set any more.

4. disable the result set.

Mysql_free_result (resource $ res );

For example, after I have used up the above, I can close the result set, mysql_free_result ($ query );

5. disconnect from the database

Mysql_close (resource $ link_identifier );

For example, in the end of the above program, I can disconnect, mysql_close ($ conn );

III. Comprehensive cases are provided here

   "; // Echo" $ row [0] -- $ row [1] -- $ row [2] -- $ row [3] -- $ row [4] "; // var_dump ($ row); // method 2 foreach ($ row as $ key => $ value) {echo "$ value --" ;}}// 6. release the resource mysql_free_result ($ res); mysql_close ($ conn); // This statement can be left blank. we recommend that you write

IV. details

1. resources must be released after $ res result set is used
2. if mysql_close () does not exist, the system will automatically close it.
3. if mysql_close is placed before the while clause, the result will not be affected because after the mysql_query statement is executed, the program has read the database to the memory and read it from the memory again. However, if mysql_free_result is put in front, an error will occur.
4. $ row is an array.
5. $ res: if a dql statement is executed, a resource type is returned. if a dml statement is executed, the bool value is returned.

6. when getting row data from $ res, there are three methods besides mysql_fetch_row ($ res) [return index array:

Mysql_fetch_assoc ($ res) -------- returns an associated array

Mysql_fetch_array ($ res) --------- the returned index array and associated array consume a lot of memory. we recommend that you do not use

Mysql_fetch_object ($ res) --------- returns a row of data as an object



So far, using the MySQL extension library to operate the database is basically complete. this is the basis for using the Mysqli Extension Library to operate the database, and the next article writes the Mysqli Extension Library to operate the database.

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.