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 ();