0

I have this array

$pv->orderRecordsArray = array();


foreach($order->records as $i=>$orderRecord){

$pv->orderRecordsArray[] = $orderRecord->orderRecordID;  
        }

// print_r($pv->orderRecordsArray) for example
// shows Array ( [0] => 46839 [1] => 46840 [2] => 46841 )

I need to use the array values from above in my sql statement below.

$sql = "
    SELECT 
    *               
         FROM 
    table1               
    WHERE 
    orderRecordID IN (46741, 46742) 
         ";

so infront of IN I want $pv->orderRecordsArray results.

thanks

2 Answers 2

3

You can use implode to generate such a list:

$sql = "SELECT *
        FROM table1
        WHERE orderRecordID IN (" . implode(', ', $pv->orderRecordsArray) . ")";

But you should also consider a subquery or Join of your tables.

Sign up to request clarification or add additional context in comments.

2 Comments

Don't forget about sanitation! (Probably with array_map.)
@strager: I think the values are also coming from a database query.
1
$sql = 'SELECT *
        FROM table1               
        WHERE orderRecordID IN ('.implode(',',$pv->orderRecordsArray).')';

2 Comments

Don't forget about sanitation! (Probably with array_map.)
It depends to how he launches this query ^^

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.