Jump to content

Update variable without page refresh?


Superman702

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.