Reputation: 19
How can i create an array from the result? I would like to use the array in a mysql IN() query.
//$array = array();
$get_subscategoria = mysqli_query($kapcs, "SELECT kat_id FROM termek_kategoria WHERE kat_parent = '$id'");
if(mysqli_num_rows($get_subscategoria) > 0 )
{
while($sub_kat = mysqli_fetch_array($get_subscategoria))
{
echo $sub_kat['kat_id'];
//$array[] = $sub_kat;
}
}
//print_r( $array );
Now, this code gives back 4 row ID, that works okay. I would like an array with these ID-s, like 1,2,3,4.
Upvotes: 0
Views: 930
Reputation: 222
while($sub_kat = mysqli_fetch_array($get_subscategoria))
{
$array[] = $sub_kat['kat_id'];
}
echo implode(",",$array);
it give a result like 1,2,3,4
Upvotes: 1
Reputation: 12378
For MySQL
, also you can use group_concat
, only gives you one record:
SELECT GROUP_CONCAT(kat_id) AS kat_id
FROM termek_kategoria
WHERE kat_parent = '$id'
GROUP BY kat_parent
Upvotes: 1
Reputation: 6737
Instead of:
while($sub_kat = mysqli_fetch_array($get_subscategoria))
{
echo $sub_kat['kat_id'];
//$array[] = $sub_kat;
}
use:
$array = mysqli_fetch_assoc($get_subscategoria);
Upvotes: 2