function displaySale() { $p = $_REQUEST["sid"]; include "sales.php"; $obj = new sales(); if (!$obj->displaySale($p)) { echo '{"result":0}'; } else { $row = $obj->fetch(); echo '{"result":1,"sale":['; while ($row) { echo json_encode($row); //convert the result array to json object $row = $obj->fetch(); if ($row) { echo ","; //if there are more rows, add comma } } echo "]}"; } }