query($sql); if ($result->num_rows > 0) { // Array to hold the results $rows = array(); // Fetch data from each row and add it to the array while($row = $result->fetch_assoc()) { $rows[] = $row; } // Output the result in JSON format header('Content-Type: application/json'); echo json_encode($rows); } else { echo json_encode($rows); } } // Close connection $conn->close(); ?>