Given the following code:
$sql = "SELECT * FROM items WHERE name LIKE '%?%'";
$key = 'orange';
$result = \DB::select(\DB::raw($sql), [$key]);
the result is always no records!
while by changing LIKE
to =
, it works fine:
$sql = "SELECT * FROM items WHERE name = ?";
I don't know why this is happening but I have to use RAW in this script. Can anybody figure out where is the problem?
You're failing to understand how bindings work... binding not only handles quotes and other special characters within the value, but also handles the quoting
$sql = "SELECT * FROM items WHERE name LIKE ?";
$key = '%orange%';
$result = \DB::select(\DB::raw($sql), [$key]);
and note the %
around the $key
value before you bind it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With