如何将结果数据库倒入数组并使用json发送?
<?php
$db = new PDO('mysql:host=localhost;dbname=Contact', 'root', '');
$stmt = $db->query('SELECT * FROM myfeilds');
$results = $stmt->fetchAll();
?>
答案 0 :(得分:1)
您可以使用json_encode
函数转换为json。
<?php
$db = new PDO('mysql:host=localhost;dbname=Contact', 'root', '');
$stmt = $db->query('SELECT * FROM myfeilds');
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); /* it will give you array of result */
$jsonResult = json_encode($results) ; /* it will convert into json format */
echo $jsonResult ; /* this will show in ajax success calling */
?>
了解更多json_encode
,您可以阅读手册json_encode