This article provides a detailed analysis of mysql operations using the mysqli Extension Library in PHP. For more information, see
1. use mysqli Extension Library in PHP to perform dql operations on mysql
The code is as follows:
Header ("Content-type: text/html; charset = utf-8 ");
// Use mysqli to operate mysql databases (object-oriented)
// 1. create a MySQLi object
$ Mysqli = new MySQLi ("localhost", "root", "root", "test ");
If ($ mysqli-> connect_error ){
Die ("connection failed". $ mysqli-> connect_error );
}
// 2. operate the database (send SQL)
$ SQL = "select * from user1 ";
// 3. processing result
$ Res = $ mysqli-> query ($ SQL );
// Var_dump ($ res );
// Fetch_assoc \ fetch_array \ fetch_object
While ($ row = $ res-> fetch_row ()){
Var_dump ($ row );
/* Foreach ($ row as $ val ){
Echo '--'. $ val;
}
Echo'
';*/
}
// 4. close the resource
$ Res-> free ();
$ Mysqli-> close ();
?>
The following process-oriented
The code is as follows:
Header ("Content-type: text/html; charset = utf-8 ");
$ Mysqli = mysqli_connect ("localhost", "root", "root", "test ");
If (! $ Mysqli ){
Die ("connection failed". mysqli_connect_error ());
}
$ SQL = "select * from user1 ";
$ Res = mysqli_query ($ mysqli, $ SQL );
// Var_dump ($ res );
While ($ row = mysqli_fetch_row ($ res )){
Foreach ($ row as $ val ){
Echo '-'. $ val;
}
Echo'
';
}
// Release the memory
Mysqli_free_result ($ res );
Mysqli_close ($ mysqli );
?>
2. use the mysqli Extension Library in PHP to perform dml operations on mysql
The code is as follows:
// Use the mysqli Extension Library to perform crud operations on mysql
Header ("Content-type: text/html; charset = utf-8 ");
$ Mysqli = new MySQLi ("localhost", "root", "root", "test ");
If ($ mysqli-> connect_error ){
Die ("connection failed". $ mysql-> connect_error );
}
// Add a record
// $ SQL = "insert into user1 (name, password, email, age) values ('Lucy ', md5 ('Lucy'), 'Lucy @ 163.com ', 17) ";
// Delete a record
// $ SQL = "delete from user1 where id = 80 ";
// Update a record
$ SQL = "update user1 set age = 20 where id = 7 ";
$ Res = $ mysqli-> query ($ SQL );
If (! $ Res ){
Echo "operation failed". $ mysqli-> error;
} Else {
If ($ mysqli-> affected_rows> 0 ){
Echo "successful ";
} Else {
Echo "no rows affected ";
}
}
// Close the resource
$ Mysqli-> close ();
?>
3. encapsulation
The code is as follows:
Class SqlHelper {
Private $ mysqli;
// Write to death first, and then use a file for configuration.
Private static $ host = "localhost ";
Private static $ user = "root ";
Private static $ pwd = "root ";
Private static $ db = "test ";
Public function _ construct (){
$ This-> mysqli = new MySQLi (self ::$ host, self ::$ user, self ::$ pwd, self ::$ db );
If ($ this-> mysqli-> connect_error ){
Die ("connection failed". $ this-> mysqli-> connect_error );
}
// Set the character set
$ This-> mysqli-> query ("set names utf8 ");
}
// Dql operate
Function execute_dql ($ SQL ){
$ Res = $ this-> mysqli-> query ($ SQL) or die ($ this-> mysqli-> error );
Return $ res;
}
// Dml operate
Function execute_dml ($ SQL ){
$ Res = $ this-> mysqli-> query ($ SQL) or die ($ this-> mysqli-> error );
If (! $ Res ){
Return 0; // failed
} Else {
If ($ this-> mysqli-> affected_rows> 0 ){
Return 1; // success
} Else {
Return 2; // no effect on rows
}
}
}
}
?>