<?php
/**
* Mysqli Pre-compilation
*/
1. Create a Mysqli object
$mysqli = new Mysqli ("wk.php", "root", "root", "DB_Text");
! $mysqli->connect_error or Die ("Connection failed". $mysqli->connect_error);
2. Creating Precompiled Objects
$sql = "INSERT into T_user2 (name,pwd,sex,age,birthday) VAlUES (?,?,?,?,?)";
$mysqli _stmt = $mysqli->prepare ($sql);
3, binding parameters (to pass the value, must pass the variable)
$name = "Zhao Liu";
$pwd = "123";
$sex = 1;
$age = 20;
$birthday = "1990-2-2";
$mysqli _stmt->bind_param ("Ssiis", $name, $pwd, $sex, $age, $birthday);
4. Implementation
$b = $mysqli _stmt->execute ();
if ($b) {
echo "Successful execution";
}else{
echo "Execution failed". $mysqli _stmt->error;
}
$mysqli _stmt->close ();
$mysqli->close ();
<?php
/**
* MYSQLI Pre-compilation query
* Precompiled to prevent SQL injection attacks
*/
1. Create a Mysqli object
$mysqli = new Mysqli ("wk.php", "root", "root", "DB_Text");
! $mysqli->error or Die ("Connection failed". $mysqli->connect_error);
2. Creating Precompiled Objects
$sql = "Select Id,name,age from T_user2 where ID >?";
$mysqli _stmt = $mysqli->prepare ($sql);
3, binding parameters (to the placeholder value)
$id = 55;
$mysqli _stmt->bind_param ("i", $id);
4. Binding result Set
$result = $mysqli _stmt->bind_result ($id, $name, $age);
5. Implementation
$mysqli _stmt->execute ();
6. Remove the value of the binding
while ($mysqli _stmt->fetch ()) {
echo "----$id-----$name-----$age <br>";
}
echo "****************<br>";
$id = 60;
$mysqli _stmt->bind_param ("i", $id);
$result = $mysqli _stmt->bind_result ($id, $name, $age);
$mysqli _stmt->execute ();
while ($mysqli _stmt->fetch ()) {
echo "----$id-----$name-----$age <br>";
}
$mysqli _stmt->free_result ();
$mysqli _stmt->close ();
$mysqli->close ();
?>
PHP mysqli Pre-compilation