我正在尝试使用 mysql resulset 将表创建为 JSON 字符串。我有 Flexigrid 所需的包和从同一路径中的另一个 php (table.php) 加载数据的功能。
我可以显示 Flexigrid 表,但没有显示数据。当我在浏览器中检查 table.php 时,我得到 JSON 字符串并且所有“看起来”都是正确的。如果有人可以帮助我了解我缺少什么,我分享我的代码?
表.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '123';
$db = 'nwind';
mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error!');
mysql_select_db($db) or die ('Check the Data Base');
$query = 'SELECT orderId, customerId, employeeId, orderDate FROM orders';
$resultset = mysql_query ($query);
if (!$resultset) {
$message = 'Query error: ' . mysql_error() . '\n';
$message .= 'Search: ' . $query;
die($message);
}
$json = array();
while ($object = mysql_fetch_object($resultset)) {
$json[] = array(
"Order_ID" => $object->orderId,
"Customer_ID" => $object->customerId,
"Employee_ID" => $object->employeeId,
"Order_Date" => $object->orderDate
);
}
echo json_encode($json);
?>
例子.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<link rel="stylesheet" href="../flexigrid/css/flexigrid.pack.css" type="text/css"/>
<script type="text/javascript" src="../js/jquery-1.8.2.js"></script>
<script type="text/javascript" src="../flexigrid/js/flexigrid.pack.js"></script>
</head>
<body>
<div class="flexigrid">
<table id="flex"></table>
</div>
<script type="text/javascript">
$("#flex").flexigrid({
url:'table.php',
dataType:'json',
colModel : [
{display: 'Order_ID',
name : 'orderId',
width : 60,
sortable : true,
align: 'center'},
{display: 'Customer_ID',
name : 'customerId',
width : 180,
sortable : true,
align: 'left'},
{display: 'Employee_ID',
name : 'employeeId',
width : 120,
sortable : false,
align: 'left'},
{display: 'Order_Date',
name : 'orderDate',
width : 130,
sortable : true,
align: 'left'}
],
sortname: 'orderId',
sortorder: 'asc',
usepager: true,
title: 'Orders',
useRp: true,
rp: 15,
showTableToggleBtn: true,
width: 700,
height: 200,
resizable: false
});
</script>
</body>
</html>