What i'm looking for, if its even possible, is a SQL Query that replaces this method so i don't have to do a query for each value in the array:
$array = array(1,2,3);
foreach ($array as $product) {
$stmt = $dbh->prepare("SELECT EXISTS(SELECT 1 FROM products WHERE product_id = :value LIMIT 1)");
$stmt->bindParam(':value', $product);
$stmt->execute();
if($row = $stmt->fetch())
{
...
}
}
Syntax can be wrong but something like this (basically is to return the products id from the array that doesn't have a record in the table):
$products = array(1,2,3);
$array = join(',', array_fill(0, count($products), '?'));
SELECT product_id WHERE product_id IN ($array) NOT EXISTS (SELECT 1 FROM products WHERE product_id IN ($array))
Is there a way to get this or i still need to use the loop?
$arraycame from? It is from an input or another sql ?$productscame from ?