Deep understanding of Mysqli preprocessing compilation _mysql

Source: Internet
Author: User
Tags php tutorial sql injection stmt

Remember the previous PHP point of Mysqli also wrote the pretreatment of the PHP tutorial, then just read a book, did not understand the principle, a few months later, suddenly understand a lot:
Think about it. If we're going to insert a lot of 1000 users, how do you do that for loops? Or does mysqli handle multiple SQL? No! These processing is very slow, PHP has a lot of functions to operate the MySQL database, but is to pass the SQL statements to the MySQL database, the real processing of SQL statements is Mysql,mysql database is to compile SQL statement execution, Is it necessary to compile the same SQL statement multiple times on the above two types of operations? Programmers are always smart, so they have the mysqli pretreatment technology! MYSQLI can also prevent SQL injection attacks!
Look at the following precompiled code:

Copy Code code as follows:

<?php
Create a connection
$mysqli =new mysqli ("localhost", "root", "" "," Test ");
Set MYSQLI encoding
Mysqli_query ($mysqli, "SET NAMES UTF8");
Check if the connection was 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;
Binding parameters
$stmt->bind_param ("i", $id);
Binding result Sets
$stmt->bind_result ($id, $username);
Execute Query
$stmt->execute ();
A variable that displays the result of a binding
while ($stmt->fetch ()) {
echo "section" $id. " User: ". $username." <br/> ";
}
/**************************************************************/
/*www.phpddt.com for you: The contents of the above * can be repeated to perform similar functions, do not need to compile the * *
Release results
$stmt->free_result ();
Turn off compilation statements
$stmt->close ();
Close a link to a database
$mysqli->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.