This article mainly introduces the bind_param () function usage in php, and briefly analyzes the functions, parameters, usage methods, and related precautions of the bind_param () function, for more information about bind_param () function usage in php, this article briefly analyzes the functions, parameters, usage methods, and precautions of the bind_param () function, for more information, see
This example describes the usage of the bind_param () function in php. We will share this with you for your reference. The details are as follows:
Literally, it is not difficult to understand the bound parameters. here is an example of binding parameters:
For example:
bind_param("sss", firstname,lastname, $email);
1. this function is bound to the SQL parameter and tells the database parameter value. The "sss" parameter column processes the data types of other parameters. The s character tells the database that this parameter is a string.
There are four types of parameters:
I-integer (integer)
D-double (double floating point type)
S-string (string)
B-BLOB (Boolean)
You must specify a type for each parameter.
By telling the database parameter data type, you can reduce the risk of SQL injection.
2. the firstname, lastname, and $ email mentioned above are references and cannot be directly written as strings after php5.3. to verify this conclusion, I wrote a test section here, as shown below:
$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",22);$stmt->execute();echo "News records created successfully!";$stmt->close();$conn->close();
I wrote a test program that directly writes parameters as strings. after running the program, it will pop up:
Finally, I changed the program to the following:
$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();
The above is the sample code for bind_param () function usage in php. For more information, see other related articles in the first PHP community!