Mysql where id is in array
$string="1,2,3,4,5";
$array=array_map('intval', explode(',', $string));
$array = implode("','",$array);
$query=mysqli_query($conn, "SELECT name FROM users WHERE id IN ('".$array."')");
NB: the syntax is:
SELECT * FROM table WHERE column IN('value1','value2','value3')
MySQL PHP - SELECT WHERE id = array()?
Use IN
.
$sql = 'SELECT *
FROM `table`
WHERE `id` IN (' . implode(',', array_map('intval', $array)) . ')';
MySQL, PHP: Select * from table where id is not in array
SOLVE:
$sql2 = "SELECT server_id, time_checked,id from server_status where time_checked<'$date' order by server_id;";
$result2=$conn->query($sql2);
while($row2 = $result2->fetch_assoc()){
while($row2 = $result2->fetch_assoc()){
$server_id = $row2['server_id'];
$id = $row2['id'];
$dt = date('Y-m-d', strtotime($row2['time_checked']));
$all[$server_id][$dt] = $id; // ARRAY
}
}
$stack = array();
$keys = array_keys($all);
for($i = 0; $i < count($all); $i++) {
foreach($all[$keys[$i]] as $key => $value) {
array_push($stack, $value);
}
}
$ids = join(',',$stack);
$sql = "SELECT * FROM server_status WHERE time_checked<'$date' AND id NOT IN ($ids)";
$result=$conn->query($sql);
echo "Server status data has been deleted.<br>";
Created another array from the multi-dimensional array to store the ids only and use NOT IN like John Green suggested.
Thanks!
PHP/MySQL - How to select id from table and echo in array
Add all the elements to an array, then implode()
it into one string with your desired deliminator (here, its ", "
) once all results are fetched.
$result = [];
$query = mysqli_query($conn, "SELECT * FROM tableA");
while($row = mysqli_fetch_assoc($query)){
$result[] = $row['id_add_user']);
}
echo implode(", ", $result);
MySQL PHP - SELECT WHERE id = array()
You can use IN() in combination with GROUP BY and HAVING
SELECT
product_id
FROM
your table
WHERE
category_id IN (1, 2, 4)
GROUP BY
product_id
HAVING
COUNT(DISTINCT id) = 3;
Explanation
With COUNT (DISTINCT id) = you check that this product_id will be in every category you checked.
DEMO