1, use mysqli operation MySQL
Example 1. Object oriented style
The code is as follows |
Copy Code |
<?php $mysqli = new Mysqli ("localhost", "My_user", "My_password", "World"); /* Check Connection * * if (Mysqli_connect_errno ()) { printf ("Connect failed:%sn", Mysqli_connect_error ()); Exit (); } printf ("Host Information:%sn", $mysqli->host_info); /* Close Connection * * $mysqli->close (); ?> /** * Above is to establish a connection with MySQL. * Host: For MySQL host address type is character type. * Username: for MySQL login username type is character. * passwd: The MySQL login password type is character type. * DB: is a character type for the MySQL database name. * Port: The port number for the MySQL database. */ $sSQL = "SELECT * from DB"; /* The SQL statement that needs to be executed * * $query = Mysqli_query ($connect, $sSQL); /* $connect: Link is a source of links $sSQL: Query also refers to the SQL statement that needs to be executed */ while ($arr = Mysqli_fetch_array ($query)) { /* $query: The results of cyclic $query * * Print_r ($arr); /* Enter the corresponding value of the field * * } Mysqli_free_result ($query); /* Release result set * * Mysqli_close ($connect); /* Close Database connection/* |
2, use MySQL operation MySQL
In PHP, this task is done through the mysql_connect () function.
Grammar
Mysql_connect (Servername,username,password);
Example
In the following example, we store a connection in a variable ($con) in a script for later use. If the connection fails, the "Die" section is executed:
The code is as follows |
Copy Code |
<?php $con = mysql_connect ("localhost", "Peter", "abc123"); if (! $con) { Die (' Could not connect: '. Mysql_error ()); } Some code ?> /** * Above is to establish a connection with MySQL. * Host: For MySQL host address type is character type. * Username: for MySQL login username type is character. * passwd: The MySQL login password type is character type. * Port: The port number for the MySQL database. */ mysql_select_db ("db"); /* Select Database DB is the database name */ $sSQL = "SELECT * from DB"; /*sql Query Statement * * $query = mysql_query ($sSQL); /* $sSQL: Query also refers to the SQL statement that needs to be executed */ while ($arr = Mysql_fetch_array ($query)) { /* $query: The results of cyclic $query * * /* Enter the corresponding value of the field * * Print_r ($arr); } Mysql_free_result ($query); /* Release result set * * |
Close connection
As soon as the script finishes, the connection closes. Use the Mysql_close () function if you want to close the connection in advance.
The code is as follows |
Copy Code |
<?php $con = mysql_connect ("localhost", "Peter", "abc123"); if (! $con) { Die (' Could not connect: '. Mysql_error ()); } Some code Mysql_close ($con); ?> |
3. Using mysqli in OOP mode
code is as follows |
copy code |
$conn = new Mysqli (" host "," username "," passwd "," DB ", port); /* * * above is to establish a connection with MySQL. * Host: is a character type for the MySQL host address type. * Username: Character type for MySQL login username. * passwd: Character type for the MySQL login password. * DB: is a character type for the MySQL database name. * Port: The port number for the MySQL database. */ $sSQL = "SELECT * from user"; /*sql Query statement */ $query = $conn->query ($sSQL); /* $sSQL: Query also refers to the SQL statement to be executed */ while ($arr = $query->fetch_array ()) { /* $query: Loop $ Results executed by query */ print_r ($arr); } $query->close (); /* Release result set */ $conn->close (); * Close Database connection */ |