Can I use something like a binding variable in Ravel?SQL non-numeric error

Asked 2 years ago, Updated 2 years ago, 52 views

I tried to select using the code below, but an error occurred.

I'd like to run a SQL query by connecting specific parameters from another variable to a SQL statement. Is there a good way?

error messages:

ErrorException:Anon-number value encountered in file

code:

$test=config('const.TEST');//(int)3
$sql="SELECT* FROM TEST WHERE col="+$test;
\DB::select($sql);

laravel

2022-09-30 20:23

1 Answers

It's very basic, so I recommend you to go around the manual first.

Basically, using the Query Builder instead of raw SQL is more like a framework.

$rows=DB::table('TEST') ->where('col', $test)->get();

To write raw SQL:

$rows=DB::select('SELECT* FROM TEST WHERE col=?', [$test]);


2022-09-30 20:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.