1. Use mysqli extended library Preprocessing Technology mysqli stmt to add three users to the database
<? 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 = " Xiaofang " ;
// $ Password = MD5 ("ffff"); $ Password = " 123456 " ; $ Email = " Xiaofang@126.com " ; $ Age =18 ; // Parameter binding->? Number Assignment here the type and order must be consistent $ Mysqli_stmt-> bind_param ( " SSSI " , $ Name, $ password, $ email, $ age ); // Run $ B = $ mysqli_stmt-> Execute (); // Add more $ Name = " Xiao Yang " ; $ Password = " 123456 " ; $ Email = " Xiaoyang@126.com " ; $ Age = 18 ; // Parameter binding->? Number Assignment here the type and order must be consistent $ Mysqli_stmt-> bind_param (" SSSI " , $ Name, $ password, $ email, $ age ); // Run $ B = $ mysqli_stmt-> Execute (); // Add more $ Name = " Small g " ; $ Password = " 123456 " ; $ Email = " XiaoG@126.com " ; $ Age = 18 ; // Parameter binding->? Number Assignment here the type and order must be consistent $ Mysqli_stmt-> bind_param ( " SSSI " , $ Name, $ password, $ email, $ age ); // Run $ 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
<? PHP // Use the pre-processed query ID> 5 user ID name email $ 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 result set $ Mysqli_stmt -> Bind_result ( $ ID , $ Name , $ Email ); // Run $ Mysqli_stmt -> Execute (); // Retrieve the bound value While ( $ Mysqli_stmt -> Fetch ()){ Echo "<Br/> $ ID -- $ Name -- $ Email " ;} // Close resource // release result $ Mysqli_stmt -> Free_result (); // Close and compile statements $ Mysqli_stmt -> Close (); // Close connection $ Mysqli -> Close (); ?>