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

iostankov's avatar

L5. Bindings is not working with DB::statement

Laravel 5.

DB::statement(‘CREATE DATABASE ?’, [‘database’]);

Error: Illuminate\Database\QueryException with message ‘SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘?’ at line 1 (SQL: CREATE DATABASE database)’

But:

DB::statement(‘CREATE DATABASE database’);

Everything is OK. Why bindings is not working?

0 likes
5 replies
mehany's avatar

I see you are using apostrophe like this

  ’         instead of      '

can you try this statement instead

DB::statement('CREATE DATABASE ?', ['database']);
iostankov's avatar

No, it just copied wrong from tinker console. I'm using right apostrophes, also tried to do it with ". Same error. I thought it was a problem because i had no doctrine installed, but it is not.

dneykov's avatar

One year ago did someone have fix for that? I have same problem here is the error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1 (SQL: CREATE DATABASE :db)

What I'm trying is:

DB::statement("CREATE DATABASE :db", ['db' => 'test_laravel']);
dneykov's avatar

I found the answer. Actually you can't use bind parameters on CREATE or DROP. Have to be used like that e.g.:

DB::statement("CREATE DATABASE {$newDatabase}");
3 likes
ericbarber's avatar

I'm attempting to CREATE a table using a statement like the following:

        $phone = Auth::user()->number;
        DB::statement('CREATE TABLE `{$phone}` (
            `id` INT NOT NULL AUTO_INCREMENT,
            `serviceType` VARCHAR(60) NULL,
            `miles` INT NULL,
            `created_at` TIMESTAMP NULL DEFAULT NULL,
            `updated_at` TIMESTAMP NULL DEFAULT NULL,
            PRIMARY KEY (`id`));');

The table it creates is titled "{$phone}" though. I realize that I cannot use bindings to pass the variable in (I tried many many times before finding this answer). Any ideas?

Laravel 5.5

Please or to participate in this conversation.