JSON encode MySQL results

0
Akbar Khan

$sth = mysqli_query($conn, “SELECT …”);
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
————————————————–

$result = mysqli_query($conn, “SELECT …”);
$rows = mysqli_fetch_all($result); // list arrays with values only in rows
// or
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC); // assoc arrays in rows

print json_encode($rows);

Add a Comment
Write your answer.
Join for Teach