$dbが\PDO
のインスタンスであると仮定します$q = "SELECT username FROM Users WHERE age = :age";
$stmt = $db->prepare($q);
$stmt->bindParam(':age', 44, PDO::PARAM_INT);
$stmt->execute();
$objects = array();
while ($object = $stmt->fetch(PDO::FETCH_OBJ)) {
$objects[] = $object;
}
echo json_encode($objects);