laravel - Binding on `statement` not supported in Eloquent 5.1? -
i'm new laravel , trying string query in eloquent. trying use db::statement
, kept getting errors placeholders in query string. seems either don't have syntax right, or bindings unimplemented or unsupported?
the reason want use statement
because i'm doing insert... select
, haven't been able find documentation in eloquent.
here's code:
$ php artisan tinker psy shell v0.5.2 (php 5.6.13-0+deb8u1 — cli) justin hileman >>> echo \db::statement('create database :db', [':db'=>'test']); illuminate\database\queryexception message 'sqlstate[42000]: syntax error or access violation: 1064 have error in sql syntax; check manual corresponds mysql server version right syntax use near '?' @ line 1 (sql: create database :db)' >>> \db::statement('create database ?', ['test']); illuminate\database\queryexception message 'sqlstate[42000]: syntax error or access violation: 1064 have error in sql syntax; check manual corresponds mysql server version right syntax use near '?' @ line 1 (sql: create database test)'
these 2 syntax forms (?
, :string
) pdo. other methods in db
such select
, insert
support this, according documentation.
the relevant parts of these errors near '?' @ line 1 (sql: create database :db)
, near '?' @ line 1 (sql: create database test)
. mysql thinks there unbound ?
in query string. didn't use syntax in first query. i'm concluding that bind()
method did not correctly bind placeholders.
this question on laracasts asking syntax, there no accepted answer.
edit 1 answer says statement()
doesn't support create
. tried queries out select
, , got same results, both placeholders:
>>> \db::statement('select 1 \'a\' = ?', array('a')); illuminate\database\queryexception message 'sqlstate[42000]: syntax error or access violation: 1064 have error in sql syntax; check manual corresponds mysql server version right syntax use near 'where 'a' = ?' @ line 1 (sql: select 1 'a' = a)' >>> \db::statement('select 1 \'a\' = :letter', array(':letter'=>'a')); illuminate\database\queryexception message 'sqlstate[42000]: syntax error or access violation: 1064 have error in sql syntax; check manual corresponds mysql server version right syntax use near 'where 'a' = ?' @ line 1 (sql: select 1 'a' = :letter)'
actually, can use create
, drop
query in db::statement()
, named bindings not used in way.
here queries success.
drop
, create
not accept bindings.
>>> \db::statement('create database test') => true >>> \db::statement('drop database test') => true
do not use backslash , single quotes in statement
>>> \db::statement('insert users (id, name) values (?, ?)', ['1', 'john']) => true
db::statement()
return ture
when success, if want see select
results, should use db::select()
>>> \db::statement('select * users') => true >>> \db::select('select * users') => [ {#770 +"id": 1, +"name": "john", }, ]
remove leading :
in second argument.
>>> \db::statement('update users set name = :name id = :id', ['id' => 1, 'name' => 'john']) => true
you affect rows if use db::update
, db::delete
>>> \db::delete('delete users id = :id', ['id' => 1]) => 1
Comments
Post a Comment