Sample code sharing for Bind_param () function usage in PHP

Source: Internet
Author: User
This article mainly introduces PHP Bind_param () function usage, simple analysis of the function of the Bind_param () function, parameters, use methods and related considerations, the need for friends can refer to the following

This example describes the use of the Bind_param () function in PHP. Share to everyone for your reference, as follows:

It's literally not difficult to understand, the parameters of the binding; Let me tell you through an example of a binding parameter:

For example:

Bind_param ("SSS", Firstname,lastname, $email);

1. The function binds the parameters of SQL and tells the values of the database parameters. The SSS parameter column handles the data type of the remaining parameters. The S character tells the database that the parameter is a string.

The following four types of parameters are available:

I-integer (integral type)
D-double (double-precision floating-point type)
S-string (String)
B-blob (Boolean)

Each parameter requires a specified type.

By telling the data type of a database parameter, you can reduce the risk of SQL injection.

2. Above the Firstname,lastname, $email is quoted, after php5.3 is not directly written into the string, in order to verify this conclusion, I wrote a test, as follows:

$servername = "localhost"; $username = "root"; $password = "admin"; $dbname = "Test"; $conn =new mysqli ($servername, $ Username, $password, $dbname), if ($conn->connect_error) {die ("Connected failed:". $conn->connect_error);} $sql = "INSERT into user (user_first,user_last,age) VALUES (?,?,?)"; $stmt = $conn->prepare ($sql); $stmt->bind_param ("sss", "Xiao", "Hong"), $stmt->execute (); echo "News Records created successfully! "; $stmt->close (); $conn->close ();

I wrote a test program that writes parameters directly to a string and pops up after running:

Finally, I rewrite the program as follows:

 $servername = "localhost"; $username = "root"; $password = "password"; $dbname = " Test "; $conn =new mysqli ($servername, $username, $password, $dbname); if ($conn->connect_error) {die (" Connect failed: ". $conn->connect_error);} $sql = "INSERT into user (user_first,user_last,age) VALUES (?,?,?)"; $stmt = $conn->prepare ($sql) $stmt->bind_param ("SSS", $user _first, $user _last, $age); $user _first= "Xiao"; $user _last= "Hong", $age =12, $stmt->execute (); echo "News Records created successfully!"; $stmt->close (); $conn->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.