tags:

views:

25

answers:

2

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

+3  A: 

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.

Gumbo
Don't forget about sanitation! (Probably with `array_map`.)
strager
@strager: I think the values are also coming from a database query.
Gumbo
+1  A: 
$sql = 'SELECT *
        FROM table1               
        WHERE orderRecordID IN ('.implode(',',$pv->orderRecordsArray).')';
MatTheCat
Don't forget about sanitation! (Probably with `array_map`.)
strager
It depends to how he launches this query ^^
MatTheCat