PDO::ERRMODE_EXCEPTION )); }catch (Exception $e){ die("Error : ".$e->getMessage()); } $response = array(); if (isset($_GET['id'])) { $req = $db->prepare('SELECT * FROM records WHERE id=:id'); $req->execute(array( "id"=>$_GET['id'] )); if ($data = $req->fetch()) { $response['records'] = array(); fill_record(0, $data); } else { $response['status'] = "Not found"; $response['message'] = "Can't fetch data for this record id."; } } else { $req = $db->prepare('SELECT * FROM records'); $req->execute(); $result = $req->fetchAll(); $i = 0; foreach($result as $row) { fill_record($i, $row); $i++; } } echo json_encode($response);