In the previous article mysqli basic knowledge of mysqli installation and basic operations (mainly a single SQL statement query operations), today is a very important part of mysqli: preprocessing.
Its three main classes are often involved in mysqli operations: The Mysqli class, the Mysql_stmt class, and the Mysqli_result class. Preprocessing is mainly done by using the Mysql_stmt class.
Preprocessing is an important means to prevent SQL injection, which is of great significance to improve the security of the website.
In this case, the database name is test, the data table name is test, the field has ID, title two, id from the growth of the primary key.
To perform an insert operation using MYSQLI preprocessing:
Connect_errno) { "Connect Error:". $mysqli->connect_error;} $mysqli->set_charset (' UTF8 '); $id = "; $title = ' title4 ';///use? Replace the variable $sql= "INSERT test VALUES (?,?)"; /Get $mysqli_stmt object, must remember to pass $sql, preprocessing is the preprocessing of SQL statement. $mysqli _stmt= $mysqli->prepare ($sql);//The first parameter indicates a variable type, with I (int), d (Double), S (string), B (BLOB) $mysqli _stmt->bind_ Param (' is ', $id, $title);//execute Pre-processing statement if ($mysqli _stmt->execute ()) { echo $mysqli _stmt->insert_id;} else{ echo $mysqli _stmt->error;} $mysqli->close ();
Prevent SQL injection with mysqli preprocessing:
$id = ' 4 '; $title = ' title4 '; $sql = "SELECT * FROM Test WHERE id=? and title=? "; $mysqli _stmt= $mysqli->prepare ($sql) $mysqli _stmt->bind_param (' is ', $id, $title); if ($mysqli _stmt-> Execute ()) { $mysqli _stmt->store_result (); if ($mysqli _stmt->num_rows () >0) { echo "verified success"; } else{ echo "validation failed"; }} $mysqli _stmt->free_result (); $mysqli _stmt->close ();
To execute a query statement using MYSQLI preprocessing:
$sql = "Select Id,title from Test WHERE id>=?"; $mysqli _stmt= $mysqli->prepare ($sql) $id =1; $mysqli _stmt->bind_param (' i ', $id); if ($mysqli _stmt->execute ( ) { $mysqli _stmt->store_result (); Binds a variable to a prepared statement for the result store $mysqli _stmt->bind_result ($id, $title); while ($mysqli _stmt->fetch ()) { echo $id. ': '. $title. '
'; }}
For more mysqli technology, please refer to the official PHP manual, which is the best way to learn.