2017-02-14 39 views
0

送我怎麼能倒導致分貝陣列和使用JSON發送?我怎麼能倒在陣列結果DB和使用JSON

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

要結果轉換爲JSON – gaurav

+0

是的,我想發送的結果,讓JS和打印這樣的: – Toprex

+0

$。每個(單,功能(I ,數據){ $ orders.append('

  • name:'+ data.name +'family:'+ data.family +'
  • '); – Toprex

    回答

    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