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

konrms's avatar

Query to select data from database table and array

How can I select data with raw query from data that exists both in database table and an array?

For example I want to store to $sc the result of the following query. The variable $cs is an array which has data from previous query execution.

$sc = DB::select(DB::Raw("select table1.object from table1, today_date where today_date.today <= table1.expiry_date and today_date.today >= table1.start_date and table1.id = $cs"));

0 likes
2 replies
Nash's avatar

Nothing in your query suggests that you actually need a raw query for this. Take a look at the query builder docs and update your code accordingly, particularly look at select, where and whereIn (for the array). Using a variable in your raw query like you are doing is dangerous and makes your application vulnerable to SQL injection

konrms's avatar

@NASH - I understand the risk. But is it possible to be done with Raw query?

Please or to participate in this conversation.