1. Use mysqli extended library Preprocessing Technology mysqli stmt to add three users to the database
Copy codeThe Code is as follows:
<? Php
// Mysqli extended library Preprocessing Technology mysqli stmt adds three users to the database
// 1. Create a mysqli object
$ Mysqli = new MySQLi ("localhost", "root", "root", "test ");
If ($ mysqli-> connect_error ){
Die ($ mysqli-> conncet_error );
}
// 2. Create a pre-compiled object
$ SQL = "insert into user1 (name, password, email, age) values (?,?,?,?) ";
$ Mysqli_stmt = $ mysqli-> prepare ($ SQL );
// Bind Parameters
$ Name = "Xiao Fang ";
$ Password = "123456 ";
$ Email = "xiaofang@126.com ";
$ Age = 18;
// Bind the parameter->? Number Assignment here the type and order must be consistent
$ Mysqli_stmt-> bind_param ("sssi", $ name, $ password, $ email, $ age );
// Execute
$ B = $ mysqli_stmt-> execute ();
// Continue adding
$ Name = "Xiao Yang ";
$ Password = "123456 ";
$ Email = "xiaoyang@126.com ";
$ Age = 18;
// Bind the parameter->? Number Assignment here the type and order must be consistent
$ Mysqli_stmt-> bind_param ("sssi", $ name, $ password, $ email, $ age );
// Execute
$ B = $ mysqli_stmt-> execute ();
// Continue adding
$ Name = "small G ";
$ Password = "123456 ";
$ Email = "xiaoG@126.com ";
$ Age = 18;
// Bind the parameter->? Number Assignment here the type and order must be consistent
$ Mysqli_stmt-> bind_param ("sssi", $ name, $ password, $ email, $ age );
// Execute
$ B = $ mysqli_stmt-> execute ();
If (! $ B ){
Echo "operation failed". $ mysqli_stmt-> error;
} Else {
Echo "operation successful ";
}
// Disable pre-Compilation
$ Mysqli_stmt-> close ();
$ Mysqli-> close ();
?>
2. Use pre-processing to query user id name email with id> 5
Copy codeThe Code is as follows:
<? Php
// Use the user id name email with the pre-processing query id> 5
$ Mysqli = new MySQLi ("localhost", "root", "root", "test ");
If ($ mysqli-> connect_error ){
Die ($ mysqli-> connect_error );
}
// Create a pre-compiled object
$ SQL = "select id, name, email from user1 where id>? ";
$ Mysqli_stmt = $ mysqli-> prepare ($ SQL );
$ Id = 5;
// Bind Parameters
$ Mysqli_stmt-> bind_param ("I", $ id );
// Bind the result set
$ Mysqli_stmt-> bind_result ($ id, $ name, $ email );
// Execute
$ Mysqli_stmt-> execute ();
// Retrieve the bound value
While ($ mysqli_stmt-> fetch ()){
Echo "<br/> $ id -- $ name -- $ email ";
}
// Close the resource
// Release result
$ Mysqli_stmt-> free_result ();
// Close and compile the statement
$ Mysqli_stmt-> close ();
// Close the connection
$ Mysqli-> close ();
?>