Key points for connecting MySQL to php and key points for mysqlphp
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:
1: ddl: Data Definition Language.
2: dml: Data Language (such as CURD );
3: dql: Data Query Language. (Such as select)
4: dtl: Data transaction language.
).
5: Receive and process the returned results.
6: disconnect.
The sample code is as follows:
<Pre name = "code" class = "php"> <? 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 mysql_select_db ("test") to operate; // 3: send the SQL command mysql_query ("set names utf8"); // set the query encoding $ SQL = "select * from test1"; $ res = mysql_query ($ SQL, $ con ); // 4: returned 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 the resource 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.