2013-01-22 40 views
0

如何使用json php和mysql顯示數據庫中的數據。 我一直在嘗試,但我沒有贏,但如果我不包括jquery它顯示,但在一個有趣的方式。 我會很感激的任何幫助。 貝婁是我到目前爲止嘗試過的。如何使用json顯示來自mysql的數據

<?php 
require_once('dbbox.php'); 
?> 
<?php 
$sql = "SELECT * FROM 
m1debtors INNER JOIN 
m1dtrans 
ON m1debtors.name = m1dtrans.user"; 
$result = mysql_query($sql, $con) or die(mysql_error($con)); 
while ($row = mysql_fetch_array($result)){ 
$accnum = $row['accnum']; 
$name = $row['name']; 
$addr1 = $row['addr1']; 
} 
// The JSON standard MIME header. 
header('Content-type: application/json'); 
$array = array('Name'=>$name, 'user'=>$user); 
echo json_encode($array); 
?> 
Here is the html page to display data 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script> 
$(document).ready(function(){ 
$.getJSON("view.php",{$name}); 
}); 
</script> 
+0

[**請不要使用'mysql_ *'功能在新代碼**] (http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – vanneto

+0

嘗試這些鏈接http://www.9lessons.info/2009/10/json-jquery-ajax-php.html或http://stackoverflow.com/questions/327231/best-way-to-display-data-通過-JSON-使用,jquery的 –

+0

好,謝謝,我會改變 – Musa

回答

0

view.php

<?php 
    require_once('dbbox.php'); 

    $sql = "SELECT * FROM m1debtors 
      INNER JOIN m1dtrans 
      ON m1debtors.name = m1dtrans.user"; 
    $result = mysql_query($sql, $con) or die(mysql_error($con)); 
    while ($row = mysql_fetch_array($result)){ 
    $accnum = $row['accnum']; 
    $name = $row['name']; 
    $addr1 = $row['addr1']; 
    } 

    // The JSON standard MIME header. 
    header('Content-type: application/json'); 
    $array = array('name'=>$name, 'user'=>$user); 
    echo json_encode($array); 
?> 

的index.php

<html> 
    <head> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script> 
     $(document).ready(function(){ 
     $.getJSON('view.php', function(data) { 
      $('#myJson').html(data.name + ' ' + data.user); 
     }); 
     }); 

    </script> 
    </head> 
    <body> 
    <div id="myJson"></div> 
    </body> 
</html> 
+0

感謝,但ID不起作用 – Musa

+0

Musa

+0

這就是我究竟做了什麼,但我使用的PHP代碼是sampe頁面是一個問題? – Musa

相關問題