Preprocessing technology of Mysqli extended library mysqli stmt

Source: Internet
Author: User
Tags stmt

The question raised ?

Now you need to add a user to the MySQL database , how do I do that ?

Ideas :

    1. Add A user to the database by using the for loop for the first time .
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.