Key points for connecting PHP to MySQL data. The following describes how to use the MySQL extension library to connect to MySQL data in PHP: 1. obtain the connection. 2. select the library. 3: Set the operation encoding. 4: sending SQL commands (key points for connecting MySQL data using MySQL PHP
To operate a MySQL database, follow these steps:
1: Get the connection.
2: Select the library.
3: Set the operation encoding.
4: send SQL commands (MySQL database can be divided into four commands:
4.1: ddl: Data Definition Language.
4.2: dml: Data language (such as CURD );
4.3: dql: data query language. (Such as select)
4.4: dtl: Data Transaction Language.
5: receive and process the returned results.
6: disconnect.
The sample code is as follows:
The code is as follows:
// 1: connect to the database
$ Con = mysql_connect ("localhost", "root", "toor ");
// If no connection is successful, an error is returned.
If (! $ Con ){
Echo "connection failed ";
Exit ();
}
// 2: Select the database to operate
Mysql_select_db ("test ");
// 3: send SQL commands
Mysql_query ("set names utf8"); // sets the query encoding.
$ SQL = "select * from test1 ";
$ Res = mysql_query ($ SQL, $ con );
// 4: return results (returned by row traversal)
While ($ row = mysql_fetch_row ($ res )){
Echo "$ row [0] ------------- $ row [1] ----------- $ row [2] ----------- $ row [3] ----------- $ row [4]".'
';
}
// 5: release resources and close the connection
Mysql_free_result ($ res );
Mysql_close ($ con );
?>
It should be noted that all operations here are performed in the memory and cannot be taken for granted, understanding some underlying things has some benefits.
Follow these steps to operate the MySQL database using the MySQL extension Library: 1: obtain the connection. 2: Select the library. 3: Set the operation encoding. 4: send SQL commands (MySQL count...