我试图通过关于获得从MySQL JSON字符串,并准备好应对highcharts教程的工作,但它不工作,我不知道为什么....我的服务器配置可以停止json的工作吗?
HTML:
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var chart;
$(document).ready(function() {
$.getJSON("data.php", function (json) {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'Revenue vs. Overhead',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: 'Amount'
},
plotLines: [
{
value: 0,
width: 1,
color: '#808080'
}
]
},
tooltip: {
formatter: function() {
return '<b>' + this.series.name + '</b><br/>' +
this.x + ': ' + this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: json
});
});
});
});
</script>
</head>
<body>
<script src="js/highcharts.js"></script>
<script src="js/exporting.js"></script>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
<?php
$con = mysql_connect("*****", "*****", "*****");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*****", $con);
$sth = mysql_query("SELECT revenue FROM projections_sample");
$rows = array();
$rows['name'] = 'Revenue';
while ($r = mysql_fetch_array($sth)) {
$rows['data'][] = $r['revenue'];
}
$sth = mysql_query("SELECT overhead FROM projections_sample");
$rows1 = array();
$rows1['name'] = 'Overhead';
while ($rr = mysql_fetch_assoc($sth)) {
$rows1['data'][] = $rr['overhead'];
}
$result = array();
array_push($result, $rows);
array_push($result, $rows1);
print json_encode($result, JSON_NUMERIC_CHECK);
mysql_close($con);
?>
:
JSON字符串正在使用该文件
data.php产生210
如果我手动输入JSON字符串到data.php图表按预期方式显示。
[{
"name": "Revenue",
"data": [23987, 24784, 25899, 25569, 25897, 25668, 24114, 23899, 24987, 25111, 25899, 23221]
}, {
"name": "Overhead",
"data": [21990, 22365, 21987, 22369, 22558, 22987, 23521, 23003, 22756, 23112, 22987, 22897]
}]
我假设我的连接信息是正确的,因为如果我故意输入错误密码,我得到了MySQL连接错误......
“Could not connect: Access denied for user ‘*****’@'localhost’ (using password: YES)”
而且,我得到“True”
返回当我添加“echo @mysql_ping() ? ‘true’ : ‘false’;”
到页面的底部。
因为我相信它应该工作我想知道是否有任何与服务器配置有关的事情可能会阻止我使用JSON?
*必须:*本'mysql_ *'功能将被已废弃在PHP 5.5](http://php.net/manual/en/faq.databases.php#faq.databases.mysql .deprecated)。不建议编写新代码,因为它将来会被删除。相反,无论是[MySQLi](http://php.net/manual/en/book.mysqli.php)还是[PDO](http://php.net/manual/en/book.pdo.php)和[是一个更好的PHP开发人员](http://jason.pureconcepts.net/2012/08/better-php-developer/)。 –
请在执行查询后检查mysql错误:'$ result = mysql_query(“SELECT overhead FROM projections_sample”); if(!result)print mysql_error;'它是调试的第一步.. – immulatin
打印json编码数据时实际正在打印什么?尝试添加'alert(json)'到javascript,看看它是否返回有效的json! –