Skip to content
Advertisement

select not equal too with loop in php

I have an array with 6 random id. How can I perform select * where id not equal to from my array in php?

I’m trying with a for loop but at first it returns everything except id[0], the second time everything except id[1] and so on.

My query look like this

select * from challenges where id <> id[i]

my array is catogary_id and looks like

Array ( [0] => 6 [1] => 2 [2] => 4 [3] => 10 [4] => 3 [5] => 5 [6] => 8 [7] => 1 [8] => 7 )

The loop

for($cnt=0;$cnt<count($cat_id);$cnt++)
{
    $task_id=$catogary_id[$cnt];
    $result = mysqli_query($connection,"select * from task id <> $task_id");
    $Qno=1;
    while($row=mysqli_fetch_assoc($result))
    {
        do something here
    }
}

Advertisement

Answer

 $sql = "SELECT *
         FROM task 
         WHERE id NOT IN ( '" . implode( "', '" , $cat_id ) . "' )";
$result = mysqli_query($connection, $sql);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement