Analysis on the use of PHP mysqli extension library Preprocessing Technology

Source: Internet
Author: User

1. Use mysqli extended library Preprocessing Technology mysqli stmt to add three users to the database
Copy codeThe Code is as follows:
<? Php

// Mysqli extended library Preprocessing Technology mysqli stmt adds three users to the database
// 1. Create a mysqli object
$ Mysqli = new MySQLi ("localhost", "root", "root", "test ");
If ($ mysqli-> connect_error ){
Die ($ mysqli-> conncet_error );
}
// 2. Create a pre-compiled object
$ SQL = "insert into user1 (name, password, email, age) values (?,?,?,?) ";
$ Mysqli_stmt = $ mysqli-> prepare ($ SQL );

// Bind Parameters
$ Name = "Xiao Fang ";
$ Password = "123456 ";
$ Email = "xiaofang@126.com ";
$ Age = 18;

// Bind the parameter->? Number Assignment here the type and order must be consistent
$ Mysqli_stmt-> bind_param ("sssi", $ name, $ password, $ email, $ age );

// Execute
$ B = $ mysqli_stmt-> execute ();

// Continue adding

$ Name = "Xiao Yang ";
$ Password = "123456 ";
$ Email = "xiaoyang@126.com ";
$ Age = 18;

// Bind the parameter->? Number Assignment here the type and order must be consistent
$ Mysqli_stmt-> bind_param ("sssi", $ name, $ password, $ email, $ age );

// Execute
$ B = $ mysqli_stmt-> execute ();

// Continue adding

$ Name = "small G ";
$ Password = "123456 ";
$ Email = "xiaoG@126.com ";
$ Age = 18;

// Bind the parameter->? Number Assignment here the type and order must be consistent
$ Mysqli_stmt-> bind_param ("sssi", $ name, $ password, $ email, $ age );

// Execute
$ B = $ mysqli_stmt-> execute ();

If (! $ B ){
Echo "operation failed". $ mysqli_stmt-> error;
} Else {
Echo "operation successful ";
}
// Disable pre-Compilation
$ Mysqli_stmt-> close ();
$ Mysqli-> close ();
?>

2. Use pre-processing to query user id name email with id> 5
Copy codeThe Code is as follows:
<? Php

// Use the user id name email with the pre-processing query id> 5
$ Mysqli = new MySQLi ("localhost", "root", "root", "test ");
If ($ mysqli-> connect_error ){
Die ($ mysqli-> connect_error );
}

// Create a pre-compiled object
$ SQL = "select id, name, email from user1 where id>? ";
$ Mysqli_stmt = $ mysqli-> prepare ($ SQL );

$ Id = 5;
// Bind Parameters
$ Mysqli_stmt-> bind_param ("I", $ id );
// Bind the result set
$ Mysqli_stmt-> bind_result ($ id, $ name, $ email );
// Execute
$ Mysqli_stmt-> execute ();

// Retrieve the bound value
While ($ mysqli_stmt-> fetch ()){
Echo "<br/> $ id -- $ name -- $ email ";
}

// Close the resource
// Release result
$ Mysqli_stmt-> free_result ();
// Close and compile the statement
$ Mysqli_stmt-> close ();
// Close the connection
$ Mysqli-> close ();

?>

Related Article

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.