Deep understanding of mysqli preprocessing and compilation _ MySQL

Source: Internet
Author: User
Deep understanding of mysqli preprocessing and compilation bitsCN.com

I remember that I used to write a php Tutorial on mysqli preprocessing in php. at that time, I only read a book and did not understand the principle. after a few months, I suddenly understood a lot:
Think about it. If we want to insert many 1000 users, what do you do, for loop? Or does mysqli process multiple SQL statements? No! These processes are slow. There are many functions in php that operate mysql databases. they are nothing more than passing SQL statements to mysql databases. what really processes SQL statements is mysql, mysql databases need to compile SQL statements for execution. the above two operations will compile the same SQL statement multiple times. is this necessary? Programmers are always very smart, so with mysqli preprocessing technology! Mysqli can also prevent SQL injection attacks!
Take a look at the following pre-compiled code:

// Create a connection
$ Mysqli = new mysqli ("localhost", "root", "", "test ");
// Sets the mysqli encoding.
Mysqli_query ($ mysqli, "set names utf8 ");
// Check whether the connection is created
If (mysqli_connect_errno ()){
Printf ("Connect failed:". mysqli_connect_error ());
Exit ();
}
// Create a prepared statement
$ Stmt = $ mysqli-> prepare ("select id, username from 'user' where 'id'>? ");
/*************************************** **********************/
$ Id = 5;
// Bind parameters
$ Stmt-> bind_param ("I", $ id );
// Bind the result set
$ Stmt-> bind_result ($ id, $ username );
// Execute the query
$ Stmt-> execute ();
// Display the binding result variable
While ($ stmt-> fetch ()){
Echo "no.". $ id. "User:". $ username ."
";
}
/*************************************** ***********************/
/* Www.phpddt.com prompts you: the above * content can be repeated to execute similar functions, no need to re-compile */
// Release result
$ Stmt-> free_result ();
// Close the compilation statement
$ Stmt-> close ();
// 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.