JSON encode MySQL results
How do I use the json_encode() function in PHP with MySQL query results?
Do I need to iterate through the rows or can I just apply it to the entire results object?
Akbar Khan Posted new comment 5 Apr, 2024
How do I use the json_encode() function in PHP with MySQL query results?
Do I need to iterate through the rows or can I just apply it to the entire results object?
$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);