Application analysis of the preprocessing technology of PHP mysqli extension library _mysql

Source: Internet
Author: User
Tags prepare stmt

1, using MYSQLI Extension Library preprocessing technology mysqli stmt add 3 users to the database

Copy Code code as follows:

<?php

MYSQLI Extension Library preprocessing technology mysqli stmt add 3 users to the database
1. Create Mysqli objects
$mysqli = new Mysqli ("localhost", "root", "root", "test");
if ($mysqli->connect_error) {
Die ($mysqli->conncet_error);
}
2. Create Precompiled objects
$sql = "INSERT INTO User1 (name,password,email,age) VALUES (?,?,?,?)";
$mysqli _stmt= $mysqli->prepare ($sql);

Binding parameters
$name = "Small Fang";
$password = "123456";
$email = "xiaofang@126.com";
$age = 18;

Parameter binding-> to? Number assignment here is the same type and order
$mysqli _stmt->bind_param ("SSSI", $name, $password, $email, $age);

Perform
$b = $mysqli _stmt->execute ();

Continue to add

$name = "Xiao Yang";
$password = "123456";
$email = "xiaoyang@126.com";
$age = 18;

Parameter binding-> to? Number assignment here is the same type and order
$mysqli _stmt->bind_param ("SSSI", $name, $password, $email, $age);

Perform
$b = $mysqli _stmt->execute ();

Continue to add

$name = "small g";
$password = "123456";
$email = "xiaoG@126.com";
$age = 18;

Parameter binding-> to? Number assignment here is the same type and order
$mysqli _stmt->bind_param ("SSSI", $name, $password, $email, $age);

Perform
$b = $mysqli _stmt->execute ();

if (! $b) {
Echo "Operation failed". $mysqli _stmt->error;
}else{
echo "Successful operation";
}
Turn off precompilation
$mysqli _stmt->close ();
$mysqli->close ();
?>


2, use the preprocessing query id>5 user ID name email
Copy Code code as follows:

<?php

User ID name email with preprocessing query id>5
$mysqli =new mysqli ("localhost", "root", "root", "test");
if ($mysqli->connect_error) {
Die ($mysqli->connect_error);
}

To create a precompiled object
$sql = "Select Id,name,email from User1 where Id>?";
$mysqli _stmt= $mysqli->prepare ($sql);

$id = 5;
Binding parameters
$mysqli _stmt->bind_param ("i", $id);
Binding result Sets
$mysqli _stmt->bind_result ($id, $name, $email);
Perform
$mysqli _stmt->execute ();

To remove a bound value
while ($mysqli _stmt->fetch ()) {
echo "<br/> $id--$name--$email";
}

Close Resource
Release results
$mysqli _stmt->free_result ();
Closing and compiling statements
$mysqli _stmt->close ();
Close connection
$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.