PHP5mysqli prepare preparation statement instructions for use _ MySQL

Source: Internet
Author: User
Instructions for using the prepare preparation statement of PHP5mysqli bitsCN.com

Mysqli's support for prepare is very good for websites with large traffic volumes. it greatly reduces system overhead and ensures the stability and security of query creation. Prepare preparation statements are divided into binding parameters and binding results. we will introduce them one by one!
(1) bind parameters
See the following php code:

// Create a connection
$ Mysqli = new mysqli ("localhost", "root", "", "volunteer ");
// Check whether the connection is created
If (mysqli_connect_errno ()){
Printf ("Connect failed: % s/n", mysqli_connect_error ());
Exit ();
}
/*
* Create a prepared query statement:
*? It is a wildcard and can be used in any data with text
* It is equivalent to a template, that is, a prepared SQL statement.
*/
If ($ stmt = $ mysqli-> prepare ("insert into 'vol _ msg '(mid, content) values (?,?) ")){
/* The first parameter is the binding type. "s" refers to a string, or "I", or int. It can also be "db ",
* D indicates the double precision and floating point type, B indicates the blob type, and the second parameter is the variable.
*/
$ Stmt-> bind_param ("is", $ id, $ content );
// Assign values to variables
$ Id = "";
$ Content = "this is the inserted content ";
// Execute the prepared statement
$ Stmt-> execute ();
// Display the inserted statement
Echo "Row inserted". $ stmt-> affected_rows;
// You can add multiple statements below without prepare pre-compilation.
// Close the database link
$ Mysqli-> close ();
}
?>

The above php instance running result:
Row inserted: 1
(2). binding result: the binding result is to give the fields you bind to php variables so that these variables can be used as necessary.
See the following php code:

// Create a connection
$ Mysqli = new mysqli ("localhost", "root", "", "volunteer ");
// Sets the mysqli encoding.
Mysqli_query ($ mysqli, "set names utf8 ");
// Check whether the connection is created
If (mysqli_connect_errno ()){
Printf ("Connect failed: % s/n", mysqli_connect_error ());
Exit ();
}
// Create a prepared statement
If ($ stmt = $ mysqli-> prepare ("select mid, content from 'vol _ msg '")){
// Execute the query
$ Stmt-> execute ();
// Bind the actual variable to the prepared statement
$ Stmt-> bind_result ($ id, $ content );
// Display the binding result variable
While ($ stmt-> fetch ()){
Echo ". $ id.": ". $ content ."
";
}
// Close the database link
$ Mysqli-> close ();
}
?>

BitsCN.com

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.