2

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>

4
  • Any error in console? Commented Jun 29, 2015 at 7:44
  • Thanks for your quick answer! Just discovered the console on Chrome... Uncaught ReferenceError: reason is not defined Commented Jun 29, 2015 at 7:47
  • This is what I get whe I run the php [ { reason: "211.0", Duracion: "3.6302777701057494" }, { reason: "201.0", Duracion: "1.3116666572168469" }, { reason: "200.0", Duracion: "1.1416666507720947" } ] Commented Jun 29, 2015 at 7:48
  • Uncaught ReferenceError: reason is not defined(anonymous function) @ (index):17m.extend.each @ jquery.js:2(anonymous function) @ (index):16m.Callbacks.j @ jquery.js:2m.Callbacks.k.fireWith @ jquery.js:2x @ jquery.js:5m.ajaxTransport.send.b @ jquery.js:5 Commented Jun 29, 2015 at 7:51

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

I understand this better. Thanks a lot!!! now it works and also I understand why.
211.0-3.6302777701057494 201.0-1.3116666572168469 200.0-1.1416666507720947
1

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.

3 Comments

Thanks, I will step by step according to yor Answer. I´ll notice You on a few minutes
I should give the query another name? Notice: Undefined variable: query in C:\xampp\htdocs\byp1\dbquery1.php on line 28 [{"reason":"211.0","Duracion":"3.6302777701057494"},{"reason":"201.0","Duracion":"1.3116666572168469"},{"reason":"200.0","Duracion":"1.1416666507720947"}]
Damn, @$myarray =getArraySQL($query); but where in the name of god have you declared a variable $query. If you say inside function getArraySQL you need to check variable scopes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.