In order to use MySQL prepared statement, you need to use other three MySQL statements as follows:
- PREPARE – Prepares statement for execution.
- EXECUTE – Executes a prepared statement preparing by a PREPARE statement.
- DEALLOCATE PREPARE – Releases a prepared statement.
Let’s take a look at an example of using the MySQL prepared statement.
PREPARE stmt1 FROM 'SELECT productCode, productName FROM products WHERE productCode = ?'; SET @pc = 'S10_1678'; EXECUTE stmt1 USING @pc; DEALLOCATE PREPARE stmt1;
Here, first we used the PREPARE statement to prepare a statement. We used SELECT query with question mark (?) as a placeholder for the product code.
Next, we declared a product code variable @pc
and set it values to S10_1678
.
Then, we used the EXECUTE
statement to execute the prepared statement with product code variable@pc
.
Finally, we used the DEALLOCATE PREPARE
to release the prepared statement.