Virb Virb - 2 months ago 7
PHP Question

Prints only one value from the record

Here, I want to print those value which are in table.
So, my query is like this:

$get_role_query = "SELECT id FROM table WHERE id IN (333,1,2)";
$roleres = mysqli_query($conn,$get_role_query);
$r_id = mysqli_fetch_assoc($roleres);


Here,
333
is not in table but
1
and
2
are in table.
When I am printing this like,

print_r($r_id);


It only gives me
1
but not getting
2
which is also in table. I don't want
333
that is fine. But I am getting only
1
, not getting
2
.

UPDATED BELOW:

As you all suggest to use while loop, I did like this:

$r_role = mysqli_num_rows($roleres);

for($i=0;$i<=$r_role;$i++)
{
print_r($r_id);
}


But I am getting output like this:

Array
(
[id] => 1
)
Array
(
[id] => 1
)
Array
(
[id] => 1
)


From above query, I just want output like,

Array
(
[0] => 1
[1] => 2
)


Any suggestion would be appreciated.
Thanks.

Answer

You are not using mysqli_fetch_assoc() correctly. It fetches one record at a time. Use it as follows:

while($r_id = mysqli_fetch_assoc($roleres))
{
   print_r($r_id);
}