I have a need to build a search string using multiple rows in the result of a previous query. In the DB, I have a table that tells me of up to 3 pairings of any two items. The original items are in another table. I was thinking I could build a string from the results of the first query and then explode the string to use in a second query.
Something like this:
$query1 = mysql_db_query("select paired_item_id FROM items_pairing ip, items i WHERE i.item_id=ip.item_id and i.item_id=" . $products_id);
while ($results = mysql_fetch_array($query1)) {
$progression_str = '';
// list ids and separate ids by a comma
$progression_str .= $results['paired_item_id'] ;
if ($results['paired_item_id'])
$progression_str .= ",";
else
$progression_str .= "";
}
$progression_pieces[] = explode(',', $progression_str);
$n = sizeof($progression_pieces);
//use exploded pieces in a query string that will be used to find the item the paired_item_id referenced
$where_str .= " and (i.item_id=" . (int)$progression_pieces[0];
for ($i=1; $i < $n; $i++) {
$where_str .= " or i.item_id=" . (int)$progression_pieces[$i];
}
$where_str .= ")";
Is this the wrong approach? I'm not getting the predicted results.
Thanks!