0

I'm trying to make a simple query with PHP that returns a list of Strings with all the names of the users, but I can figure out how to do do it. The only thing i thought wa this:

<?php

$host_name  = "hostname";
$database   = "database";
$user_name  = "username";
$password   = "pass";

$connect = mysqli_connect($host_name, $user_name, $password, $database);

$select = "SELECT username from userstasker";

$result = $connect->query($select);
$rows = array();

while($r = mysqli_fetch_assoc($result)) {
    $rows[] = $r;
}

echo json_encode($rows);

$connect->close();
?>

But this returns a JSon, ant idea how to do it?

7
  • It returns a json because.... echo json_encode($rows); Commented Nov 8, 2017 at 11:01
  • What do you mean by a "list"? HTML <li> tags, or just rows of text, or what? Commented Nov 8, 2017 at 11:03
  • Sorry i was not clear, i wanted an array Commented Nov 8, 2017 at 11:04
  • $rows[] = $r['username']; Commented Nov 8, 2017 at 11:04
  • Then comment out the echo json_encode($rows); and access the array $rows Commented Nov 8, 2017 at 11:05

1 Answer 1

1
<?php

$host_name  = "hostname";
$database   = "database";
$user_name  = "username";
$password   = "pass";

$connect = mysqli_connect($host_name, $user_name, $password, $database);

$select = "SELECT username from userstasker";

$result = $connect->query($select);
$usernames = array();

while($r = mysqli_fetch_assoc($result)) {
    $usernames[] = $r["username"];
}

$connect->close();

// Do something with $usernames

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.