PHP MYSQL database access

 

PHP database in our daily development must need, MYSQL database is a lot of programmers favorite database, MYSQL Since it is an open source, with a little semi-commercial, market share is relatively high, it has been it is considered to be the best partner PHP, but PHP also has a very powerful database support capabilities, to explain the basic steps in this chapter show PHP to access MySQL database. Marble platform production plant

PHP MySQL database access basic steps as shown:

1. Connect MySQL database

Use mysql_connect () establishes a connection to the MySQL server functions. About the mysql_connect () function is used, we will be behind the detail.

2. Select the MySQL database

Use mysql_select_db () function to select database MySQL database server. And establish a connection with the database related mysql_select_db () function is used, we will have concrete Detailed behind.

3. execute SQL statements

Selecting in the database mysql_query () function SQL statement is executed, the operation mode of the data include five ways, here we were introduced.

  • Query data: using select query statement to achieve functional data.

  • Data display: Use the select statement to display the query results data.

  • Insert data: Use insert into statement to insert data into the database.

  • Updated data: Use record update statement to update the database.

  • To delete data: use the delete statement to delete records in the database!

mysql_query () describes specific functions of particular use later -

4. Close the result set

After the completion of database operations, the result needs to set off, to release the system resources, syntax is as follows:

1

mysql_free_result($result);

skill:

If you have frequent access to the database in multiple pages, you can establish a persistent connection to the database server to improve the efficiency, because every time you connect to the database server takes a long time and resource overhead Jiaotong University, persistent connections relative to He said that it would be more efficient method of establishing a persistent connection when the database is indirect, call the function mysql_pconnect () instead of mysql_connect function. Establish a continuous connection at the end of this program, no need to call mysql_colse () to close the connection to the database server. In this next program execution mysql_pconnect () function, the system automatically returns to direct continuous connection ID number has been established, and not really go to connect to the database.

5. Close the MySQL server

Do not use a mysql_connect () or mysql_query () function, the system will consume resources, and then a small amount of web sites the user is done and the problem is not large, but if more than a certain number of users connected, it will cause system performance degradation, or even crash, to avoid this phenomenon, after completion of operation of the database should be used mysql_close () function closes the connection MYSQL server to save system resources.

Syntax is as follows:

1

mysql_close($link);

Description:

PHP in connection with the non-persistent connection to the database, the system will automatically recover, generally do not set off but if one-time Fanhu result set is large, or the site was visited more than parity, it is best to use mysql_close () function manually release .

Guess you like

Origin www.cnblogs.com/furuihua/p/12172286.html