javascript - json 不显示 php 查询的任何内容

标签 javascript php html json

我对此很陌生,但我正在尝试解决这个问题。

我必须对 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/

相关文章:

html - 如何让文本区域跨越表格的两列

javascript - 如何在图像上方的列中心画一条黑线?

javascript - jQuery remove() 不删除元素

JavaScript 斐波那契 - RangeError : when base (n == 1) or (n == 0), 但不是 if (n <=)。为什么?

php - 在 https 页面上嵌入 Justin.tv 播放器

php - 如何在提交表单后刷新网页

php - mysql_result():提供的参数不是C:\AppServ\www\script short\shortener.inc.php中的有效MySQL结果资源

java - 向服务器发送 HTTP POST 请求并获得响应?

javascript - 在 IE 中使用 JavaScript 在 Windows 中可以使用任何检测 URI 方案的方法

javascript - 我们如何解决 Safari 中的国际引用错误?