Php getting started Knowledge Point 1: Connection and query between PHP and MYSql. The most important thing to learn about php is the interaction with the database.
Php getting started Knowledge Point 1: Connection and query between PHP and MYSql. The most important thing to learn about php is the interaction with the database.
1. Connect to the database
1. mysql_connect: Enable the MySQL link.
Mysql_connect ('host', 'username', 'Password ')
2. mysql_select_db: open a database
Mysql_select_db ('database name', $ link identifier) // if the link identifier is left blank, the last opened connection is used by default.
3. mysql_query ("set names 'gbk'") solves Chinese garbled characters;
Mysql_query ("set names 'Encoding (utf8 or GBK)'") // UTF8 cannot contain "-"
Ii. query data
1. mysql_query (SQL statement, connection identifier );
$ SQL = "Select * FROM Test"
$ Result = mysql_query ($ SQL) // The Connection identifier is the last opened link by default.
// Get error information
$ Result = @ mysql_query ($ SQL) or die (mysql_error ())
2. Obtain the query results.
A. mysql_fetch_row ($ result );
$ Row = mysql_fetch_row ($ result );
Echo $ row [0];
B. mysql_fetch_array ($ result );
$ Row = mysql_fetch_array ($ result );
Echo $ row [0];
Echo $ row ['key'];
Note: The functions of mysql_fetch_array and mysql_fetch_row are basically the same, except that they can be indexed at the offset starting from 0, or by domain name.
Returns all the domain values of the next row and saves them to an array. If no row exists, false is returned.