The steps for the MySQL extension library to operate the MySQL database are as follows:
1: Get connection.
2: Select the library.
3: Set the operation code.
4: Send SQL commands (MySQL database can be divided into four commands:
4.1: ddl: Data Definition Language.
4.2: dml: data manipulation language (such as CURD);
4.3: dql: data query language. (Such as select)
4.4: dtl: Data Transaction Language.
5: Receive the returned result and process it.
6: Disconnect.
The specific sample code is as follows:
Copy code The code is as follows:
//1: Connect to database
$con=mysql_connect("localhost","root","toor");
//If the connection is not successful, an error will be reported
If(!$con){
echo "Connection failed";
exit();
}
//2: Select the database to operate
Mysql_select_db("test");
//3:Send SQL command
Mysql_query("set names utf8");//Set query encoding
$sql="select *from test1";
$res=mysql_query($sql,$con);
//4: Return results (traverse and return by row)
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 memory, and nothing can be taken for granted. Therefore, it is still beneficial to understand some low-level things.