Pipebritop
Pipebritop

Reputation: 135

json doesnt show anything with php query

Im certanley new on this, but I´m Trying to solve this problem.

I have to repeat a lot this kind of query to an sql server. My problem is when i call the php from html, the getjason function or any other method return any data.

The 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);

The HTML and 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>

Upvotes: 2

Views: 92

Answers (2)

Swaraj Giri
Swaraj Giri

Reputation: 4037

Solution,

Instead of reason while looping, use val.reason

What are the other issues?

In php.php, you call getArraySQL method with $query as a parameter but the function getArraySQL definition does not take any parameters and builds some query by itself.

Morever, you are supressing errors by appending @ in front of $myarray =getArraySQL($query);

Things to look into,

  • Error suppression
  • Naming files, php.php does not make sense. It never did, it never will.

Upvotes: 1

JungleZombie
JungleZombie

Reputation: 1181

You have wrong variable name (Duration != Duracion) and you need to refer to val as an object to get the data from it.

Change your JavaScript code to

$.each(data, function (key, val) {
    $('ul').append('<li Reason="' + val.reason + '">' + val.Duracion + '</li>');
});

Also change

@$myarray = getArraySQL($query);

To

$myarray = getArraySQL();

Upvotes: 2

Related Questions