Preprocessing class stmt in Mysqli

Source: Internet
Author: User

Non-SELECT statement (with no result set)

1. Establishing a Connection database

$mysqli =new mysqli ("localhost", "root", "" "," sqldb ");

2. Judging

if (Mysqli_connect_error ()) {

echo "Failed to connect to database". Mysqli_connect_error ();

Exit

}

3.SQL Statement Assembly

$sql = "INSERT into shops (name,price,description) VALUES (?,?,?,?)";

$sql = "Update shops set name=?,price=?,num=?,description=?; where id=? ";

4. Inserting SQL statements

$stmt = $mysqli->prepare ($sql);//More convenient

5. Send a value for each symbol (binding parameter)

$stmt->bind_param ("Sdisi", $name, $price, $num, $description, $id);

6. Transfer value

$name = "Zhangsan";

$price = 22.22;

$num = 10;

$description = "very good";

$id = 10;

7. Implementation

$stmt->execute ();

Number of rows affected: $stmt->insert_id;

Last inserted ID: $stmt->offected_rows;

    $mysqli[Email protected]NewMysqli ("localhost", "root", "" "," Sqldb "); if(Mysqli_connect_error()){        Echo"Connection Database Error".$mysqli _connect_error (); }    /*Assemble Data*/    $sql= "INSERT into shop values (null,?,?,?)"; $sql= "Update shop set name=?,price=?,description=?" where id=? "; /*Inserting Data*/    $stmt=$mysqli->prepare ($sql); /*binding Data*/    $stmt->bind_param ("Sdsi",$name,$price,$description,$id); /*Assign Value*/    $name= "ZHANGSANASDFADFASDFASDF"; $price=2.3; $description= "Very good"; $id=1; /*Execution*/    $stmt->execute ();

SELECT statement (with result set)

1. Connect to the database

$mysqli =new mysqli ("localhost", "root", "" "," sqldb ");

2. Judging the database

if (Mysqli_connect_error ()) {

echo "Error:". Mysqli_connect_error ();

}

3. Splicing the database

$sql = "Select Id,name,price,description where id=?";

4. Inserting a database

$stmt = $myqli->prepare ($sql);

5. Binding the Database

$stmt->bind_param (i, $id);

6. Binding result Set

$stmt->bind_result ($id, $name, $price, $description);

7. Assigning values

$id = 10;

8. Implementation

$stmt->execute ();

9. Remove the result set

$stmt->store_result ();

10.while Viewing results

while ($stmt->fetch ()) {

echo "$id---$name---$price-$description";

}

11. Close the result set

$stmt->free_result ();

12. Close the database

$stmt->close ();

<?PHP$mysqli= @NewMysqli ("localhost", "root", "" "," Sqldb "); if(Mysqli_connect_error()){        Echo"Error:".Mysqli_connect_error(); }    /*Assemble SQL*/    $sql= "Select Id,name,price,description from shop where id<?"; /*Insert*/    $stmt=$mysqli->prepare ($sql); Var_dump($stmt); /*binding*/    $stmt->bind_param (I,$id); /*binding result Set*/    $stmt->bind_result ($id,$name,$price,$description); /*Assign Value*/    $id=10; /*Execution*/    $stmt-execute (); /*Remove result set*/    $stmt-Store_result (); Echo"<table border= ' 1 ' >"; //field information, column information    $result=$stmt-Result_metadata (); Echo"<tr>";  while($field=$result-Fetch_field ()) {        Echo"<th>{$field->name}</th> "; }        Echo"</tr>"; /*fetch () View result set*/    /*Move Database Pointers*/    //$stmt->data_seek (2);     while($stmt-Fetch ()) {        Echo"<tr>"; Echo"<td>$id</td><td>$name</td><td>$price</td><td>$description</td> "; Echo"</tr>"; }    Echo"</table>"; /*Close Result set*/    $stmt-Free_result (); /*Close Database D*/    $stmt-close ();?>

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.