Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP MySQL Prepared Statements

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:

  1. 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(?, ?, ?)
  2. The database parses, compiles, and performs query optimization on the SQL statement template, and stores the result without output.

  3. 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:


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:

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 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:

Example (PDO Using Prepared Statements)

setAttribute ( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION ) ; // prepare SQL and bind parameters $stmt = $conn -> prepare ( " INSERT INTO MyGuests (firstname, lastname, email) VALUES (:firstname, :lastname, :email) " ) ; $stmt -> bindParam ( ' :firstname ' , $firstname ) ; $stmt -> bindParam ( ' :lastname ' , $lastname ) ; $stmt -> bindParam ( ' :email ' , $email ) ; // insert row $firstname = " John " ; $lastname = " Doe " ; $email = " john@example.com " ; $stmt -> execute ( ) ; // insert other rows $firstname = " Mary " ; $lastname = " Moe " ; $email = " mary@example.com " ; $stmt -> execute ( ) ; // insert other rows $firstname = " Julie " ; $lastname = " Dooley " ; $email = " julie@example.com " ; $stmt -> execute ( ) ; echo " new records inserted successfully " ; } catch ( PDOException $e ) { echo " Error: " . $e -> getMessage ( ) ; } $conn = null ; ?>