Superman702 Posted August 9, 2011 Share Posted August 9, 2011 Another question from the newbie! Is there a way to fresh a variable from a MySQL database without refreshing the page? I was thinking of setting up a loop such as: while ($num_rows == 0){ $num_rows = mysql_num_rows($result); echo "No change yet!"; } Is that the right was of doing it, or is there a different way? Quote Link to comment Share on other sites More sharing options...
jhsachs Posted August 9, 2011 Share Posted August 9, 2011 The simple answer to your question is yes; you can execute any type of logic within a PHP script, including a loop that calls MySQL functions. But executing a tight loop that performs a test over and over is a bad idea in most cases, including this one. It uses CPU time profligately to no good purpose. In this case it loads the database as well. There are several techniques for detecting an event, depending on what type of event it is and what the application needs to do. I'd need to know more about what you're trying to accomplish to say more. For example, is the change in the database the ultimate event you're trying to detect, or is it an indication of something else, which might be indicated in a way that is more convenient for this purpose? Quote Link to comment Share on other sites More sharing options...
Superman702 Posted August 9, 2011 Author Share Posted August 9, 2011 yeah, I just uploaded that file and it just made the same statement over and over and over again....I am shocked my computer didn't crash...haha Here is my main file that I have. What I am trying to do is make a ticker for a page of mine. Kind of like an emergency broadcast system. When a new message has been added to the database to broadcast, then it will replace the current "echo". <HTML> <HEAD> <TITLE>v2.0</TITLE> <meta http-equiv="refresh" content="20"> </head> <BODY> <?php // Connects to your Database $link = mysql_connect("*****", "*****", "*****"); mysql_select_db("a7405553_test", $link); $result = mysql_query("SELECT * FROM Message", $link); $mins = time()-60; mysql_query("DELETE FROM `Message` WHERE `Timestamper` < '$mins'"); $num_rows = mysql_num_rows($result); if($num_rows == 0){ echo "<table width=\"100%\" border=\"0\"> "; echo "<tr><td width=\"10%\"><CENTER></CENTER></td>"; echo "<td width=\"80%\"><CENTER>Main Menu</CENTER></td>"; echo "<td><CENTER></CENTER></td>"; echo "</TR></table>"; } else{ $info = mysql_fetch_assoc($result); echo "<table width=\"100%\" border=\"0\"> "; echo "<tr><td width=\"10%\"><CENTER></CENTER></td>"; echo "<td width=\"80%\" bgcolor=\"red\"><marquee behavior=\"scroll\" direction=\"left\"><B><font color=\"white\">Alert: </B>".$info['Main'] . "</marquee></font></td>"; echo "<td><CENTER></CENTER></td>"; echo "</TR></table>"; } ?> </BODY> </HTML> Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 9, 2011 Share Posted August 9, 2011 You can't dynamically update the content in a web page with just PHP. You will need to implement AJAX (client-side java script with server-side PHP) to dynamically query the server and get the updated info, then use the javascript to populate the new data on the page. Quote Link to comment Share on other sites More sharing options...
Superman702 Posted August 9, 2011 Author Share Posted August 9, 2011 Well...there goes that idea then...haha Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.