如何使用json php和mysql显示数据库中的数据。 我一直在尝试,但我没有赢,但如果我不包括jquery它显示但有趣的方式。 我会感激任何帮助。 贝娄是我迄今为止所尝试过的。
<?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 :(得分: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>