简体   繁体   中英

Mysqli, SELECT FROM, php

I'm trying to do a simple SELECT username FROM users from a database

$sql = "SELECT username
FROM users";

$select_sql = mysqli_query($conn, $sql);
$parse_sql = mysqli_fetch_array($select_sql);
var_dump($parse_sql);

but it gives me this with var_dump while having multiple entries.

array(2) { [0]=> string(0) "" ["username"]=> string(0) "" }

Other queries like this are working fine

$sql = "SELECT username, password FROM users WHERE username ='$user'";
$select_sql = mysqli_query($conn, $sql);

I can't find what's wrong

Use while loop to get all the users in db_user

$sql = "SELECT username
FROM db_user";

$select_sql = mysqli_query($conn, $sql);
while($parse_sql = mysqli_fetch_assoc($select_sql))
{
  $usernames[] = $parse_sql
}

var_dump($usernames);

mysqli_fetch_array fetches only a single row from result set.

Consider using mysqli_fetch_all to get all results.

Also it is good to use results as associate array only by give

mysqli_fetch_all($select_sql, MYSQLI_ASSOC);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM