Tuesday 15 July 2014

javascript - PHP constant SQL query -


To get the latest values, I need to read my database continuously every 1 second. It is my code: / P>

  & lt ;? Connect to php // "test" 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`"; // Execute the query and return the result to $ result = $ conn- & gt; Store the query ($ sql); If (! $ Result) print 'error!'; // If $ result has at least one row ($ result-> num_rows & gt; 0) {// output line of each row resulting in $ $ ($ line = $ result-> fetch_assoc ( )) {$ Product = $ line; Print_r ($ generation); }} And {print '0 result'; } $ Conn- & gt; Close (); ? & Gt;   

My HTML code refreshes every 1 second: Reload the function () {setInterval (getData, 1000); } Function getData () {$ .get ('test.php', function) {var output = data; document.getElementById ("output"). InnerHTML = "Info:" + output;}); } .... & lt; Body onload = "reload ();" & Gt; & Lt; P id = "output" & gt; & Lt; / P & gt; & Lt; / Body & gt;

Everything works fine but after about 5-10 minutes the MYSQL server crashes, I'm assuming it is being overloaded. My idea is that I keep the php script every time that connects every second, am I doing it wrong? Any suggestions on any better implementation?

I think that when you have finished the script, you can "close" the database Looking for something. / P>

You can include a hostname by adding mysqli p: To use a so-called permanent database connection

  // Connect to the "test" database $ conn = new mysqli ('P: localhost', 'root', '', 'test');   

Read more about continuous connections here:

No comments:

Post a Comment