Ricou
Ricou

Reputation: 1026

MySQL : named parameters with PREPARE command?

Is it possible in MySQL to use the PREPARE command with named parameters such as PDO in PHP:

Here is my example:

 SET @s = 'SELECT * FROM MY_TABLE WHERE my_column_1 = ? AND my_column_2 = ? ';
 PREPARE stmt2 FROM @s;
 SET @a = 54;
 SET @b = 89';
 EXECUTE stmt2 USING @a, @b;

Is it possible to do something like that :

 SET @s = 'SELECT * FROM MY_TABLE WHERE my_column_1 = :value1 AND my_column_2 = :value2 ';

Upvotes: 18

Views: 10086

Answers (1)

scragar
scragar

Reputation: 6824

I suggest looking at the documentation regarding this. https://dev.mysql.com/doc/refman/8.0/en/prepare.html

The documentation makes no references to any other way to bind variables other than the ?s, but it does mention that you can use user defined variables.

SET @s = 'SELECT * FROM MY_TABLE WHERE my_column_1 = @a AND my_column_2 = @b ';
PREPARE stmt2 FROM @s;
SET @a = 54;
SET @b = 89';
EXECUTE stmt2;

Produces the same output and the variables are only evaluated at execution of the statement, it just lacks the explicitness of binding the variable to the query.

Upvotes: 3

Related Questions