我对此很陌生,但我正在尝试解决这个问题。
我必须对 SQL 服务器重复很多此类查询。我的问题是当我从 html 调用 php 时,getjason 函数或任何其他方法返回任何数据。
PHP
<?php
function getArraySQL()
{
$dsn = "prueba";
$connect = odbc_connect( $dsn, '', '' );
$query = " SELECT hist_statusevents.reason, Sum(hist_statusevents.duration/3600) AS 'Duracion'
FROM hist_statusevents, hist_eqmtlist, hist_exproot
WHERE hist_exproot.shiftindex = hist_statusevents.shiftindex AND hist_statusevents.shiftindex = hist_eqmtlist.shiftindex AND hist_statusevents.eqmt = hist_eqmtlist.eqmtid AND (hist_eqmtlist.eqmtid='SVEDALA') AND hist_statusevents.category In ('2')
GROUP BY hist_statusevents.reason
ORDER BY Duracion DESC";
if(!$rs = odbc_exec($connect, $query)) die();
$rawdata = array();
$i=0;
while($row = odbc_fetch_array($rs))
{
$rawdata[$i] = $row;
$i++;
}
odbc_close( $connect );
return $rawdata;
}
@$myarray =getArraySQL($query);
echo json_encode($myarray);
HTML 和 getJson
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
<script type='text/javascript' src='js/jquery.js'></script>
<title></title>
</head>
<body>
<script>
$(document).ready(function() {
$.getJSON('php.php', function(data) {
$.each(data, function(key, val) {
$('ul').append('<li Reason="' + reason + '">' + Duration + '</li>');
});
});
});
</script>
<ul></ul>
</body>
</html>
</head>
<body>
</body>
</html>
最佳答案
您的变量名称错误(Duration!= Duracion),您需要将 val 作为对象引用才能从中获取数据。
将 JavaScript 代码更改为
$.each(data, function (key, val) {
$('ul').append('<li Reason="' + val.reason + '">' + val.Duracion + '</li>');
});
也改变
@$myarray = getArraySQL($query);
至
$myarray = getArraySQL();
关于javascript - json 不显示 php 查询的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31110308/