douweilei2307 2017-02-13 23:54
浏览 57
已采纳

如何在数组中倒入结果db并使用json发送

How can I pour result db in array and send with json ?

<?php 
$db = new PDO('mysql:host=localhost;dbname=Contact', 'root', '');
$stmt = $db->query('SELECT * FROM myfeilds');
$results = $stmt->fetchAll();
?>
  • 写回答

1条回答 默认 最新

  • douhang1913 2017-02-14 00:30
    关注

    you can use json_encode function to converting into 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 */ 
    ?> 
    

    for more json_encode you can read manual json_encode

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部