Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kreng's avatar
Level 5

SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Good day everybody! Try to execute role creation in DB by using following statement

$name = 'John';
$pass = '1234';
DB::statement("CREATE ROLE ? WITH LOGIN PASSWORD '?'", [$name, $pass]);

I works fine if I post the SQL directrly into DB but in Laravel it has the following error: "SQLSTATE[HY093]: Invalid parameter number: parameter was not defined (SQL: CREATE ROLE John WITH LOGIN PASSWORD '1234' )"

What's wrong with my code?

0 likes
5 replies
bobbybouwmann's avatar

Your role name should be in string format as well

DB::statement("CREATE ROLE '?' WITH LOGIN PASSWORD '?'", [$name, $pass]);
Kreng's avatar
Level 5

It doesn't work. I hav even tried the following statements

DB::statement("CREATE ROLE :n WITH LOGIN PASSWORD ':p'", ['n' => $name, 'p' => $pass]);
DB::statement("CREATE ROLE ':n' WITH LOGIN PASSWORD ':p'", ['n' => $name, 'p' => $pass]);
COACHTHEM's avatar

For me too its not working

            $result = \DB::select(
                " select * from user where id = :id ",
                [
                    'id' => $id
                ]
            );

I get below error

SQLSTATE[HY093]: Invalid parameter number: parameter was not defined (SQL: select * from user where id = :id )

I am doing this on my homestead machine.. is it something to do with mysql configuration?

bobbybouwmann's avatar

You probably need to remove the spaces in your query!

Also you can use ? instead, so you don't have to make the key anymore

 $result = \DB::select('select * from user where id = ?', [$id]);
mohammadsherafat41's avatar

remove qoute : '?' => ?

$name = 'John'; $pass = '1234'; DB::statement("CREATE ROLE ? WITH LOGIN PASSWORD ? ", [$name, $pass]);

Please or to participate in this conversation.