0

I need to constantly read from my database every 1 second to get the latest values. Here is my code:

    <?php
    // connect to the "tests" database
    $conn = new mysqli('localhost', 'root','','Test');

    // check connection
    if (mysqli_connect_errno()) {
     exit('Connect failed: '. mysqli_connect_error());
    }

    // SELECT sql query
   $sql = "SELECT * FROM `Value`"; 

   // perform the query and store the result
   $result = $conn->query($sql);

   if (!$result)
    print 'Error!';

   // if the $result contains at least one row
   if ($result->num_rows > 0) {
    // output data of each row from $result
     while($row = $result->fetch_assoc()) {
      $output=$row;
    print_r($output);
     }
   } 
   else {
    print '0 results';
   }

   $conn->close();
   ?>

My HTML code refreshes every 1 second as follows:

function reload (){
    setInterval(getData,1000);
}

function getData()
  {
    $.get('test.php', function(data) {

        var output = data;

        document.getElementById("output").innerHTML = "Info: " + output;
    }); 
  }
....
<body onload="reload();">

<p id="output"></p>

</body>

Everything works fine but after around 5-10 mins the MYSQL server crashes. I'm assuming it is being overloaded. My thoughts are that I keep running the php script every time which connects each second. Am I doing this incorrectly? Anyone have any suggestions on a better implementation?

000
  • 26,951
  • 10
  • 71
  • 101
Mark Manickaraj
  • 1,661
  • 5
  • 28
  • 44

3 Answers3

2

I think you are looking for something to have the database "unclosed" when the script has ended.

In mysqli you can prepend the hostname by adding p: to use a so called persistant database connection

// connect to the "tests" database
$conn = new mysqli('p:localhost', 'root','','Test');

Read more about persistant connections here: http://php.net/manual/en/features.persistent-connections.php

bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72
0

You might try having it not start the next request until 1 second after the last one finishes by calling setTimeout() in the callback like so:

function getData()
  {
    $.get('test.php', function(data) {

        var output = data;

        document.getElementById("output").innerHTML = "Info: " + output;
        setTimeout(getData, 1000);
    }); 
  }
....
<body onload="getData();">

This is generally a better approach than using setInterval(), cuz you may end up having two concurrent connections and request A may start before request B, but may end after B, because something happened and it took more than a second to finish the request. This could cause some weird behavior.

This may also fix your issue, because maybe it fails because it ends up having several concurrent connections open from the same IP, etc.

CWSpear
  • 3,230
  • 1
  • 28
  • 34
0

Yes it can be implement in a much better way. As you are using same Database connection configuration every-time, there is no need to connect and close database on page refresh.Connecting to database server every-second is very expensive call.

Why don't you just reload/refresh the query statement?

The idea is:

Use Persistent Database Connection

Refer BlaM answer in following post to know why persistent connections is optimal.

Put the queries in a separate div say #load.

      echo '<div id="load">';
// SELECT sql query
   $sql = "SELECT * FROM `Value`"; 

   // perform the query and store the result
   $result = $conn->query($sql);

   if (!$result)
    print 'Error!';

   // if the $result contains at least one row
   if ($result->num_rows > 0) {
    // output data of each row from $result
     while($row = $result->fetch_assoc()) {
      $output=$row;
    print_r($output);
     }
   } 
   else {
    print '0 results';
   }
       echo '</div>';

Use jquery function to refresh only #load div

<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$("#load").load("filename.php #load");
}15000); // refresh every 1 second
</script>

I had implemented this for auto-refresh leaderboard page.It worked perfectly and server didn't crash even in 50 hrs.

Community
  • 1
  • 1
Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71