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
19 Nov 2022 at 06:21 PM
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);

0
give a positive ratinggive a negative rating
20 Nov 2022 at 11:57 AM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us