PHP mysqli Pre-compilation

Source: Internet
Author: User
Tags sql injection

<?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

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.