You can use the inbuilt php function json_encode() http://php.net/manual/en/function.json-encode.php
To encode the results use something like
<?php
$pdo = new PDO("mysql:dbname=database;host=127.0.0.1", "user", "password");
$statement = $pdo->prepare("SELECT * FROM table");
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…