Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ';
like image 721
Ricou Avatar asked Apr 12 '11 09:04

Ricou


Video Answer


1 Answers

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.

like image 188
scragar Avatar answered Oct 03 '22 18:10

scragar