How can I use mysql SET variables while using my PDO queries?

db_query('SET @x := 5,SET @y := 6, SELECT * from mytable')->fetchAll();

I have tried this and many variations of this statement with and without ':' etc, but it doesn't work.


There are 2 points here.

SET @x := 5,SET @y := 6, SELECT * from mytable" should be "SET @x := 5,SET @y := 6; SELECT * from mytable


SET @x := 5,SET @y := 6;" and "SELECT * from mytable

are 2 separate MySQL statements.

You can not execute multiple statements together using db_query(). Since the PHP functions mysql_query() and mysqli_query() can only execute one SQL statement, the db_query() wrapper can also only execute one SQL statement.

What you can do here is, divide that into two part and run it.

db_query('SET @x := 5,SET @y := 6;')->execute();
db_query('SELECT * from mytable')->fetchAll();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.