PHP mysqli preprocessing statement Two instance code

Source: Internet
Author: User
Tags mysql tutorial php tutorial prepare stmt

PHP tutorial MySQL Tutorial I preprocessing statements two instance code


<?php
/*=========================MYSQLI_STMT preprocessing class (recommended) =========================*/
/*=============================== Advantages: High efficiency, safety ================================*/
$mysqli =new mysqli ("localhost", "root", "123456", "xiaoqiangdb");


/*================================= non-SELECT statement ==================================*/

$sql = "INSERT into shops Tutorial (NAME,PRICE,NUM,DESN) VALUES (?,?,?,?)"; Prepare a statement to put in the server

$stmt = $mysqli->prepare ($sql); Put it in the database tutorial

$stmt->bind_param ("SDIs", $name, $price, $num, $DESN); Pass value to placeholder, type-variable (cannot be a value)

$name = "Zhangsan";
$price = 56.56;
$num = 66;
$DESN = "good";

$stmt->execute (); Perform

echo "Last ID". $stmt->insert_id;
$stmt->close ();

/*=================================select Statement ==================================*/

$sql = "Select id,name,price,num,desn from shops where id>?"; Prepare a statement to put in the server

$stmt = $mysqli->prepare ($sql); Put it in the database

$stmt->bind_param ("i", $id); Pass value to placeholder, type-variable (cannot be a value)

$stmt->bind_result ($id, $name, $price, $num, $DESN); Binding result Sets

$id = 99;

$stmt->execute (); Perform

$stmt->store_result (); All the disposable results are taken in order to move the pointer and get totals.

Field information
$result = $stmt->result_metadata ();

while ($field = $result->fetch_field ()) {
Echo $field->name;
}
echo "<br>";

Record information
$stmt->data_seek (2);

while ($stmt->fetch ()) {
echo "$id--$name--$price--$num--$DESN <br>";
}
echo "Total records:". $stmt->num_rows;

$stmt->free_result ();

$stmt->close ();

?>


Example Two


<?php

$mysqli =new mysqli ("localhost", "root", "" "," Test ");
$mysqli->query ("Set names ' GBK '");//Set to Chinese encoding

$stmt = $mysqli->stmt_init ();//Return is a preprocessing class object

Prepare a statement
$sql = "INSERT INTO ' test ' (' name ', ' age ', ' DPC ') VALUES (?,?,?)"; /Prepare a statement
$stmt->prepare ($sql);

In fact Mysqli itself has prepare () method, so, the above part can be changed to:

Prepare a statement
$sql = "INSERT INTO ' test ' (' name ', ' age ', ' DPC ') VALUES (?,?,?)"; /Prepare a statement

$stmt = $mysqli->prepare ($sql)//directly returns the Stmt object

Binding parameters by placeholder
$stmt->bind_param ("Sis", $name, $age, $DPC);//"Sis" refers to the data type of the parameter s: string, I: Integer, D: Double precision, B: Sophomore system

Insert data repeatedly
$name = "Chen 1";//Fill in Parameters
$age = "25";//Fill in Parameters
$DPC = "Web Development note Writer, url is www.chhua.com";//Fill in Parameters
$stmt->execute ()//execution

$name = "Chen 2";//Fill in Parameters
$age = "25";//Fill in Parameters
$DPC = "Web Development note Writer, url is www.chhua.com";//Fill in Parameters
$stmt->execute ()//execution

$name = "Chen 3";//Fill in Parameters
$age = "25";//Fill in Parameters
$DPC = "Web Development note Writer, url is www.chhua.com";//Fill in Parameters
$stmt->execute ()//execution

$name = "Chen 4";//Fill in Parameters
$age = "25";//Fill in Parameters
$DPC = "Web Development note Writer, url is www.chhua.com";//Fill in Parameters
$stmt->execute ()//execution

echo "Last inserted ID:". $stmt->insert_id. " <br> ";
echo "The last statement affected the number of rows:". $stmt->affected_rows;
$stmt->


Preprocessing code


<?php
 
$mysqli =new mysqli ("localhost", "root", "", "Test");
$mysqli->query ("Set Names" GBK ' ");//Set to Chinese encoding
 
//Prepare a statement
$sql =" SELECT * from ' Test ' where ' id ' ";"; /prepares a statement
 
$stmt = $mysqli->prepare ($sql);//Directly returns the Stmt object
 
//By placeholder binding parameters
$stmt->bind_ Param ("i", $id);//"I" refers to the data type of the parameter s: string, I: Integer, D: Double precision, B: Sophomore system
$id =5
$stmt->execute ();//execute
$stmt->bind_ Result ($id, $name, $age, $DPC)//binding results Note that you must correspond to the
$stmt->fetch () for the name of the segment queried in the query statement;//This method is a one-piece fetch of result set information
while ($ Stmt->fetch ()) {
 echo $name. " ----". $age." ----". $dpc." <br> ";
}
$stmt->close ();//Close preprocessed object
?

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.