The question raised ?
Now you need to add a user to the MySQL database , how do I do that ?
Ideas :
- Add A user to the database by using the for loop for the first time .
- Use bulk Add
$sql 1= "Insert xxx";
$ssql. = "Insert xxx";
。。。
Use of $msyqli->mutil_query ($sql 1);
3. The scenario uses precompiled technology , which can also prevent SQL injection attacks .
<?PHP//Pre-compiled demo//Requirements: Use preprocessing to add three users to a database//1. Create a Mysqli object$mysqli =NewMysqli ("localhost","Root","hsp123","Test"); //2. Creating Precompiled Objects$sql ="insert INTO User1 (name,password,email,age) VALUES (?,?,?,?)"; $mysqli _stmt =Prepare , $mysqli($sql)Or Die ($mysqlierror); //prepare ($sql) preprocessing functions //Binding Parameters$name ="Xiao Qian"; $password="Xiaoqian"; $email="[email protected]"; $age=" $"; //parameter binding, assign value, here the type and order are to correspond. //$mysqli _stmt->bind_param ("SSSI", $name, $password, $email, $age); //Execution$b = $mysqli _stmt->execute (); //continue to add$name ="Old Demon"; $password="Laoyao"; $email="[email protected]"; $age=" About"; $mysqli _stmt->bind_param ("SSSI", $name, $password, $email, $age); //Execution$b = $mysqli _stmt->execute (); //continue to add$name ="Vegetable Layer"; $password="AAA"; $email="[email protected]"; $age=" -"; $mysqli _stmt->bind_param ("SSSI", $name, $password, $email, $age); //Execution$b = $mysqli _stmt->execute (); if(!$b) {Die ("operation failed". $mysqli _stmt->error); }Else{echo"Operation OK"; } //Release$mysqliclose ();?>
Now let's look at some of the other common functions of mysqli
Case : Write a function, receive a table name, and then put the table header and table data, display the page
<?phpfunction showtable ($table _name) {$mysqli=NewMysqli ("localhost","Root","hsp123","Test"); if(Mysqli_connect_error ()) {Die (Mysqli_connect_error ()); } //$sql = "SELECT * from $table _name";$sql ="desc User1"; $res= $mysqliquery ($sql); //How to get the total number of rows and columns returnedEcho"Total Rows". $res->num_rows."-column =". $resField_count; Echo"<table border= ' 1 ' ><tr>"; //How to remove the table head from the $res while($field = $resFetch_field ()) {echo"<th>{$field->name}</th>"; } Echo"</tr>"; //Loop out Data while($row = $resFetch_row ()) {echo"<tr>"; foreach($row as$val) {echo"<td> $val </td>"; } Echo"</tr>"; } Echo"</table>"; //Close Resource$resFree (); $mysqli-Close (); } showtable ("User1");?>
Preprocessing technology of Mysqli extended library mysqli stmt