0

I have the following Laravel, 5.4, DB query, which generates an error:

$item = \DB::select("SELECT :id FROM :tab WHERE :id1 = :value",['id' => $parameters[3],'id1' => $parameters[3],'tab' => $parameters[2],'value' => $value]);

I need to know any way that allows me to printout the SQL statement after preparation and before execution. i.e after supplying the values of the placeholders to understand where is the error comes from.

I tried to use DB::connection()->enableQueryLog(); like the following:

\DB::connection()->enableQueryLog();
                $item = \DB::select("SELECT :id FROM :tab WHERE :id1 = :value",['id' => $parameters[3],'id1' => $parameters[3],'tab' => $parameters[2],'value' => $value]);
                dd(DB::getQueryLog());

It does not do any thing and sql error like:

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 '? WHERE ? = ?' at line 1 (SQL: SELECT :id FROM :tab WHERE :id1 = :value)

Is there any method for the DB class that prints the statement with its values? i.e I need to see values instead of ?

2
  • What version of laravel 5 are you using? Commented Jul 16, 2017 at 22:49
  • @RossWilson Version 5.4 Commented Jul 16, 2017 at 23:34

1 Answer 1

1

Try to post a dump of all the injected parameters to the query, it's probably an empty value or string value without quotes.

In general, I recommend you to work with Laravel's query builder, it's much simpler and its error is more indicative:

DB::table($parameters[2])
    ->select([$parameters[3]])
    ->where($parameters[3], $value)
    ->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.