What is parameterized query?

A parameterized query is a query in which placeholders are used for parameters and the parameter values are supplied at execution time.

Why use Parameterized Query

  1. The most important reason to use parameterized queries is to avoid SQL injection attacks.
  2. Secondly parameterized query takes care of scenario where sql query might fail for e.g. inserting of O'Baily in a field. Parameterized query handels such query without forcing you to replace single quotes with double single quotes.

This statement is one of features of the database system in which same SQL statement executes repeatedly with high efficiency. The prepared statements are one kind of the Template and used by application with different parameters.Reference Article

Database System can execute the same SQL statement without doing the parsing, compiling and optimizing again and again for the same kind of SQL Statement.

You can write or create prepared statement in MySQL but this is not an efficient way because the binary protocol through a prepared statement API is better.

But still you can write and even this doesn’t require any other programming you can directly write in SQL. You can use a prepared statement for MySQL Client program.You can also use a prepared statement in a stored procedure for the dynamic SQL approach.

Create prepared statement in MySQL: reference is taken from this article

PREPARE TestStmt FROM 
'SELECT * FROM Test 
WHERE TestNumber=?';

You can use PHP code to manage prepared statement through its API or manage at the level of JDBC.


A parameterized query (also known as a prepared statement) is a means of pre-compiling a SQL statement so that all you need to supply are the "parameters" (think "variables") that need to be inserted into the statement for it to be executed. It's commonly used as a means of preventing SQL injection attacks.

You can read more about these on PHP's PDO page (PDO being a database abstraction layer), although you can also make use of them if you're using the mysqli database interface (see the prepare documentation).


This is a clear and succinct explanation of what it is, and how it works. How and Why to use Parameterization [archive link] (since the original link is dead)

Essential the process involves the server preprocessing the request without parameters so it knows the type of query it is. So, for example a SELECT query is only a SELECT query, and cannot be concatenated by a parameter(request variable) to be a SELECT / DROP or some other MySql injection. Instead the injection data will be just string data in the parameter field.