0

I am selecting a single column from an SQL table. I want to get as result an array of usernames in json. I don't know what to write in the missing code place in the code below.

Desired result for example:

users ["userA", "userB", "userC"];

<?php
    require_once 'connection.php';
    $result = $conn->query("SELECT username FROM users");
    $users = array();
    // missing code
    $json = json_encode($users);
    echo $json;
?>
1
  • 1
    Try change it to $users = $result->fetch_all(MYSQLI_NUM); Commented Jun 23, 2019 at 18:53

1 Answer 1

2

You can fetch all of the users using...

$users = $result->fetch_all(MYSQLI_NUM);

Using MYSQLI_NUM will return a numerically indexed list and so json_encode() will work as you wish.

Sign up to request clarification or add additional context in comments.

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.