Check if a list of ids all exist in MySQL and PHP -
what efficient way in mysql
, php
check if list of ids exist? want function return result true
if all ids exists, else false
.
i thinking:
$ids = array(2233, 5545, 9478, 5343, 3545); do_all_groups_exist($ids); function do_all_groups_exist(array $ids = array()) { if(empty($ids)) { return true; } $sql = "select count(`id`) count groups `id` in (" . implode(',', $ids) . ")"; ... $row = mysqli_fetch_object($result); return (intval($row->count) === count($ids)) ? true : false; }
is there better way?
you can result in sql statement itself
$countids= count($ids); $sql= "select case when ( select count(id) cnt groups id in (" . implode(',', $ids) . ") )=$countids 'true' else 'false' end result" ... $row=mysqli_fetch_array($result); echo $row['result'];
you result coloumn name 'result'
it return true when ids exists in table.
Comments
Post a Comment