Key points for connecting PHP to MySQL DATA and phpmysql data
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:
Copy codeThe Code is as follows:
<? Php
// 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]". '<br/> ';
}
// 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.