我如何将数组传递给mysql语句[关闭]

ok so i have a problem i need to pass an array of product ids to my sql statement below

$index = 0;
  $products = $cart->get_products();
  for ($i=0, $n=sizeof($products); $i<$n; $i++) {
   $prod = $products[$index]['id'];

 if (strpos($prod,"{")){
 $product = preg_split("^[{}]^",$prod);
 }else{
 $product = $prod;
 }
 }
$product_query = tep_db_query("select cb.products_id, SUM(cb.customers_basket_quantity),SUM(p.products_rate)  from " . TABLE_PRODUCTS . " p INNER JOIN " . TABLE_CUSTOMERS_BASKET . " cb ON p.products_id = cb.products_id   
WHERE cb.customers_id ='" . $customer_id ."'
AND p.products_id IN '".$product ."'
");

the results are always

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Array'' at line 3

select cb.products_id, SUM(cb.customers_basket_quantity),SUM(p.products_rate) from products p INNER JOIN customers_basket cb ON p.products_id = cb.products_id WHERE cb.customers_id ='3' AND p.products_id IN 'Array'

make it like this IN (1, 2, 3, 4, 5) using implode(',', $product);

(Be VERY careful about what you're inserting into your SQL if you're making it yourself (instead of PDO params etc)!)

Make sure $product is an array:

$product = (array)$product;

And then escape its values:

$product = array_map('your_db_escape_function', $product);

And then stringify it:

$product = "'" . implode("', '", $product) . "'";

And then add it to the query:

"... p.products_id IN (" . $product . ")"

After your code edit, I assume this is what you want:

$products = $cart->get_products();
$productIds = array_map(function($product) {
    return (int)$product['id'];
}, $products);
$productIds = implode(', ', $productIds);

$query = tep_db_query("
    select cb.products_id, SUM(cb.customers_basket_quantity),SUM(p.products_rate)
    from " . TABLE_PRODUCTS . " p
    INNER JOIN " . TABLE_CUSTOMERS_BASKET . " cb ON p.products_id = cb.products_id
    WHERE cb.customers_id ='" . $customer_id ."'
    AND p.products_id IN (" . $productIds . ")
");

ok figured it out! it was a variable that i needed so it now reads

$id = $this->rate($id);
   $qty = $cart->count_contents($id);

which returns the proper qty in my formula!