0

I don't quite understand how to fetch data from MySQL database using ajax. I've read a lot through internet but still I'm not able to find an answer.

  <script src="js/jQuery.js"></script>
  <script type="text/javascript">
  setInterval(function(){
  $(function ()
  {

    $.ajax({
      url: 'api.php',
      data: "",
      dataType: 'json',
      success: function(data)
      {
          console.log(data);
        }
      });
    })
  }, 1000);

  </script>
<?php
  include("connect.php");
  $link=Connection();
  $result = mysqli_query($link, "SELECT * FROM `estacionamiento' ORDER BY timeStamp DESC");
  $data = array();
  while($row = mysql_fetch_assoc($result)){
    $data[] = $row;
  }

  echo json_encode($data);
  var_dump($data);

?>  

My table 'estacionamiento' is structured by 4 columns which are (timeStamp, dir1, dir2, state). So from my above code, I want to receive these variables that will be changing over the time, so that's why I put setInterval. As far as I know, when I encode json those variables, it sends an object to my javascript... but I need them as string variables in my javascript. Also I'm not able to see anything in web console where I'm supposed to see because of console.log(data). Finally, I want to know if there's a way to see if I'm sending correctly $data from php code.

Btw, I've done this before but just sending one variable and It was by execute statement php. I'm not familiar with this code.

<?php
    include("connect.php");
    $link=Connection();
    $stmt = $link->prepare("SELECT estado FROM estacionamiento ORDER BY timeStamp DESC"); /* PREPARE YOUR QUERY */
    $stmt->execute(); /* EXECUTE QUERY */
    $stmt->bind_result($estado); /* BIND RESULT TO THIS VARIABLE */
    $stmt->fetch(); /* FETCH RESULT */
    $stmt->close(); /* CLOSE PREPARED STATEMENT */

    echo $estado;

?>

EDIT: The error visible in Chrome's developer tools:

MySQL errors shown in network tab

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • Have you watched the AJAX request / response in the browser's developer tools? Are there any errors reported? Are you running this on a web-server? – Jay Blanchard Jun 09 '16 at 21:23
  • 1
    With the var_dump in there, you're currently going to be outputting stuff that isn't valid JSON. – Jonnix Jun 09 '16 at 21:24
  • @JayBlanchard Yes, here is my link http://estacionamiento.site88.net/. I haven't seen any error. – Jeancarlo C. Jun 09 '16 at 21:25
  • If you look at the response errors from your requests your MySQL is failing with this error: *"Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in /home/a6528460/public_html/api.php on line 4*" – Jay Blanchard Jun 09 '16 at 21:26
  • @JonStirling Alright, didn't know that. I've just erased it but still can't see anything – Jeancarlo C. Jun 09 '16 at 21:26
  • *"Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a6528460/public_html/api.php on line 6"* – Jay Blanchard Jun 09 '16 at 21:27
  • You're mixing `mysql_*` and `mysqli_*` functions, which doesn't work. – Jay Blanchard Jun 09 '16 at 21:27
  • @JayBlanchard Where do u see that? I'm using web console from chrome – Jeancarlo C. Jun 09 '16 at 21:27
  • `\`estacionamiento'` <--- watch for quotes. – Jonnix Jun 09 '16 at 21:27
  • I've given you quite a bit to go on here, so I am closing your question as a duplicate. All of this can be seen in the network tab of your developer tools. [Basic lesson in troubleshooting with developer tools.](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Jun 09 '16 at 21:28
  • Also, double check connect.php. You probably do not need `$link` as there is a connection already established. If you'd like you can post that code here as well. the good news is your AJAX appears to work perfectly! – Jay Blanchard Jun 09 '16 at 21:33

0 Answers0