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 ();
?>