PHP MySQL Prepared Statements
Prepared statements are very useful for preventing MySQL injection.
Prepared Statements and Bound Parameters
Prepared statements are used to execute multiple identical SQL statements with higher execution efficiency.
The working principle of prepared statements is as follows:
-
Preparation: Create an SQL statement template and send it to the database. Reserved values are marked with the parameter "?". For example:
INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)
-
The database parses, compiles, and performs query optimization on the SQL statement template, and stores the result without output.
-
Execution: Finally, the application passes the bound values to the parameters (the "?" markers), and the database executes the statement. The application can execute the statement multiple times if the parameter values differ.
Compared to directly executing SQL statements, prepared statements have two main advantages:
-
Prepared statements greatly reduce analysis time, performing the query only once (even though the statement is executed multiple times).
-
Bound parameters reduce server bandwidth; you only need to send the query parameters, not the entire statement.
-
Prepared statements are very useful against SQL injection because parameter values are sent using a different protocol, ensuring data validity.
MySQLi Prepared Statements
The following example uses prepared statements in MySQLi and binds the corresponding parameters:
Example (MySQLi Using Prepared Statements)
connect_error ) { die ( " Connection failed: " . $conn -> connect_error ) ; } // prepare and bind $stmt = $conn -> prepare ( " INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?) " ) ; $stmt -> bind_param ( " sss " , $firstname , $lastname , $email ) ; // set parameters and execute $firstname = " John " ; $lastname = " Doe " ; $email = " john@example.com " ; $stmt -> execute ( ) ; $firstname = " Mary " ; $lastname = " Moe " ; $email = " mary@example.com " ; $stmt -> execute ( ) ; $firstname = " Julie " ; $lastname = " Dooley " ; $email = " julie@example.com " ; $stmt -> execute ( ) ; echo " new records inserted successfully " ; $stmt -> close ( ) ; $conn -> close ( ) ; ?>Analyze each line of code in the example below:
"INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)"
In the SQL statement, we use a question mark (?). Here we can replace the question mark with an integer, string, double-precision float, or boolean.
Next, let's look at the bind_param() function:
$stmt->bind_param("sss", $firstname, $lastname, $email);
This function binds the SQL parameters and tells the database the parameter values. The "sss" parameter indicates the data types of the remaining parameters. The s character tells the database that the parameter is a string.
The parameters have the following four types:
- i - integer
- d - double
- s - string
- b - BLOB (binary large object)
Each parameter needs to specify a type.
By specifying the data type of parameters to the database, the risk of SQL injection can be reduced.
![]() |
Note: If you want to insert other data (user input), validation of the data is very important. |
|---|
Prepared Statements in PDO
In the example below, we use prepared statements in PDO and bind parameters:
