Php Mysqli preprocessing statement two instance code

Source: Internet
Author: User
Tags mysql tutorial

Php tutorial mysql tutorial I preprocessing statement two instance code


<? Php
/* ===================================== Mysqli_stmt preprocessing class (recommended) ======================================= */
/* =, Security ===================================== */
$ Mysqli = new mysqli ("localhost", "root", "123456", "xiaoqiangdb ");
 
 
/* = ======================================= */
 
$ SQL = "insert into shops tutorial (name, price, num, desn) values (?,?,?,?) "; // Prepare a statement to be placed on the server
 
$ Stmt = $ mysqli-> prepare ($ SQL); // database placement tutorial
 
$ Stmt-> bind_param ("sdis", $ name, $ price, $ num, $ desn); // pass the value to the placeholder, type-variable (cannot be a value)
 
$ Name = "zhangsan ";
$ Price = 56.56;
$ Num = 66;
$ Desn = "good ";
 
$ Stmt-> execute (); // execute
 
Echo "last id". $ stmt-> insert_id;
$ Stmt-> close ();

/* = ===================================== */

$ SQL = "select id, name, price, num, desn from shops where id>? "; // Prepare a statement to be placed on the server
 
$ Stmt = $ mysqli-> prepare ($ SQL); // put it in the database
 
$ Stmt-> bind_param ("I", $ id); // send a value to the placeholder, type-variable (cannot be a value)
 
$ Stmt-> bind_result ($ id, $ name, $ price, $ num, $ desn); // bind the result set
 
$ Id = 99;
 
$ Stmt-> execute (); // execute
 
$ Stmt-> store_result (); // only the results of one-time lecture can be obtained to move the pointer and obtain the total number.
 
// 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 number of records:". $ stmt-> num_rows;
 
$ Stmt-> free_result ();
 
$ Stmt-> close ();

?>


Instance 2


<? Php
 
$ Mysqli = new mysqli ("localhost", "root", "", "test ");
$ Mysqli-> query ("set names 'gbk'"); // set it to Chinese encoding.
 
// $ Stmt = $ mysqli-> stmt_init (); // The returned object is a preprocessing object.
 
// Prepare a statement
// $ SQL = "insert into 'test' ('name', 'age', 'dpc') values (?,?,?) "; // Prepare a statement
// $ Stmt-> prepare ($ SQL );
 
// In fact, mysqli itself has the prepare () method. Therefore, the above part can be changed:
 
// Prepare a statement
$ SQL = "insert into 'test' ('name', 'age', 'dpc') values (?,?,?) "; // Prepare a statement
 
$ Stmt = $ mysqli-> prepare ($ SQL); // The stmt object is directly returned.
 
// Bind parameters by placeholder
$ Stmt-> bind_param ("sis", $ name, $ age, $ dpc); // "sis" refers to the parameter data type s: String, I: integer, d: Double precision, B: Large binary
 
// Insert data repeatedly
$ Name = "Chen Hua 1"; // enter the Parameter
$ Age = "25"; // enter the Parameter
$ Dpc = "web Development note writer at www.chhua.com"; // enter the Parameter
$ Stmt-> execute (); // execute
 
$ Name = "Chen Hua 2"; // enter the Parameter
$ Age = "25"; // enter the Parameter
$ Dpc = "web Development note writer at www.chhua.com"; // enter the Parameter
$ Stmt-> execute (); // execute
 
$ Name = "Chen Hua 3"; // enter the Parameter
$ Age = "25"; // enter the Parameter
$ Dpc = "web Development note writer at www.chhua.com"; // enter the Parameter
$ Stmt-> execute (); // execute
 
$ Name = "Chen Hua 4"; // enter the Parameter
$ Age = "25"; // enter the Parameter
$ Dpc = "web Development note writer at www.chhua.com"; // enter the Parameter
$ Stmt-> execute (); // execute
 
Echo "Last inserted id:". $ stmt-> insert_id. "<br> ";
Echo "the last statement affects the number of rows:". $ stmt-> affected_rows;
$ Stmt->


Preprocessing code


<? Php
 
$ Mysqli = new mysqli ("localhost", "root", "", "test ");
$ Mysqli-> query ("set names 'gbk'"); // set it to Chinese encoding.
 
// Prepare a statement
$ SQL = "select * from 'test' where 'id'>? "; // Prepare a statement
 
$ Stmt = $ mysqli-> prepare ($ SQL); // The stmt object is directly returned.
 
// Bind parameters by placeholder
$ Stmt-> bind_param ("I", $ id); // "I" refers to the parameter data type s: String, I: integer, d: Double precision, B: large binary
$ Id = 5;
$ Stmt-> execute (); // execute
$ Stmt-> bind_result ($ id, $ name, $ age, $ dpc); // note that the binding result must correspond to the segment name queried in the query statement.
$ Stmt-> fetch (); // This method obtains the result set information one by one.
While ($ stmt-> fetch ()){
Echo $ name. "----". $ age. "----". $ dpc. "<br> ";
}
$ Stmt-> close (); // close the preprocessing object
?>

Related Article

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.