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.
0
give a positive ratinggive a negative rating
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);

Share on FacebookShare on TwitterShare on LinkedInSend email
1 answer
x
x
2024 AnswerTabsTermsContact us