Hannah Pink
Hannah Pink

Reputation: 81

PHP - how to add array values in mysql query and retrieve single data from each category

i have a following array output...i want to use this array value in mysql query.

Output of array value:

Array ( [0] => Array ( [0] => 144 [category_id] => 144 ) [1] => Array ( [0] => 98
[category_id] => 98 ) [2] => Array ( [0] => 146 [category_id] => 146 ) [3] => 
Array ( [0] => 142 [category_id] => 142 ) ) 

Array Value : 144 98 146 142

i want Sql query to get four records related to above ids (144 98 146 142)

$Sql_array = mysql_query("SELECT * FROM product_table")

Thanks for your help....

Upvotes: 0

Views: 364

Answers (3)

Breezer
Breezer

Reputation: 10490

Well you could use a loop like so

(string)$select;
foreach($array as $k){
 $select.= $k[0].",";
}
$select = substr_replace($select ,"",-1);
$query = "SELECT * FROM product_table WHERE id IN (".$select.")";

Upvotes: 0

Petar Sabev
Petar Sabev

Reputation: 858

Try:

SELECT * FROM product_table WHERE id IN (144,98,146,142);

Upvotes: 1

Peter
Peter

Reputation: 16923

SELECT * FROM product_table WHERE category_id IN (1,2,3,4)

in your example

$Sql_array = mysql_query("SELECT * FROM product_table WHERE category_ID IN (".join(',',$array).")")

Upvotes: 0

Related Questions