Dave599
Dave599

Reputation: 19

Php array from sql result

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

Answers (3)

praveena
praveena

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

Blank
Blank

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

user2342558
user2342558

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

Related Questions