Mysqli fetch_assoc results - show all rows in array, not 1 row - PHP
How can I show all rows using PHP mysqli fetch_assoc ? I am getting only one row in associative array, but there should be more results.
Hi,
To get all rows from SQL results into associative array, you have to use fetch_all(MYSQLI_ASSOC) instead of fetch_assoc().
Mysqli fetch_assoc works with While:
$sql = "SELECT id_user, user_name FROM users WHERE user_group = '3' ORDER BY user name;";
$result = $c->query($sql);
while ( $user = $result->fetch_assoc() ) {
echo "<br />" . $user['user_name'];
}
Mysqli fetch_all:
$sql = "SELECT id_user, user_name FROM users WHERE user_group = '3' ORDER BY user name;";
$result = $c->query($sql);
$user = $result->fetch_all(MYSQLI_ASSOC);
print_r($user);
1 answer