Sql fiddle for your convenience here.
I\'m taking data from a MySql table and turning it into a json array. All works well and I have the output the way I want it, but i
Taking out all the redundancy, using proper prepared statements (assuming PDO) and adding error handling (at least a stub), you end up with this:
$stmt = $conn->prepare('SELECT name, age, address, pincode FROM json WHERE name = ?');
$stmt->execute(array('peter'));
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo json_encode($row);
} else {
echo json_encode(array('status' => 'error'));
}
If you expect multiple rows:
echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));