0

I have an table called user like this

id | user
 1 | test
 2 | test1

I have an query like this:

$arr = array(0 => 1 , 1=> 2);
$sql = "SELECT user 
    FROM user 
    WHERE id IN (".implode(',',$arr).") 
    ORDER BY id";
$gg = mysqli_query($conn, $sql);
if(!$gg) {
    echo mysqli_error($conn);
} else {
    while ($row = mysqli_fetch_array($gg)) {
        print_r($row[0]);
    }
}

Ouput always shows as testtest1 but i want it in array like ([0] => test, [1] => test1) or even test,test1 is ok but it should work outside loop as well. Help appreciated.

3
  • and where is the error? you print the array with key 0 so add that value tu an array and it's done Commented Sep 21, 2018 at 14:43
  • The amount of keys can vary and I wont be knowing how much are there Commented Sep 21, 2018 at 14:45
  • what array do you want to create? Commented Sep 21, 2018 at 14:46

2 Answers 2

1

Create an array, then print the array. (And/or do whatever you want with the array)

E.G:

$arr = array(0 => 1 , 1=> 2);
$sql = "select user from user where id IN (".implode(',',$arr).") ORDER by id";
$gg = mysqli_query($conn, $sql);
$arrayOfResults = array();

if(!$gg){
    echo mysqli_error($conn);
}
else {
    while ($row = mysqli_fetch_array($gg))
    {
        array_push($arrayOfResults, $row[0]);
    }

    print_r($arrayOfResults);
}
Sign up to request clarification or add additional context in comments.

Comments

0

mysqli_fetch_array($gg, MYSQLI_ASSOC) will return you an associative array. you can verify using var_dump($row) also, you can use MYSQLI_NUM (for index as number) or MYSQLI_BOTH (for index as number as well as column name) instead of MYSQLI_ASSOC

otherwise you can also use mysqli_fetch_assoc e.g. here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.