PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm having trouble creating a page that retrieves data from a SQL database every X seconds and I thought this would be a good place to ask.. This is the code I have so far:
<html>
<body>
<meta http-equiv="refresh" content="5; URL=log.php">
<?php
if (isset($_COOKIE["user"]))
{
$host = -snip-
$database = -snip-
$user = -snip
$password = -snip-
$sqlhandle = mysql_connect($host, $user, $password) or die("Could not connect to server.");
$selected = mysql_select_db($database, $sqlhandle) or die("Could not connect to the database: '".$database."'.");
$history = "SELECT * FROM Chat";
$result = mysql_query($history) or die("Query failed.");
$storeres = array();
$amount = mysql_num_rows($result);
$eleval = $amount;
while($data = mysql_fetch_array($result))
{
$storeres[$eleval]["User"] = $data["User"];
$storeres[$eleval]["Message"] = $data["Message"];
$eleval = $eleval - 1;
}
for ($i=0;$i <= $amount;$i=$i+1)
{
if ( ($storeres[$i]["User"] <> NULL) and ($storeres[$i]["Message"] <> NULL) )
{
print("<b>".$storeres[$i]["User"].":</b> ");
print($storeres[$i]["Message"]."<br/>");
}
}
//Newest message at the bottom
/*while($data = mysql_fetch_array($result))
{
if ( ($data["User"] <> NULL) and ($data["Message"] <> NULL) )
{
print("<b>".$data["User"].":</b> ".$data["Message"]."<br/>");
}
}*/
}
?>
</body>
</html>
Although this works (Meta tag refreshes it every 5 seconds), I've found it to be very annoying.. Especially with a browser such as Internet Explorer which 'clicks' each refresh. I am using this page in an iframe on the main page and I need the refreshing to be discreet to the user, any ideas?
Last edited by Benjamin on Fri May 15, 2009 3:21 pm, edited 1 time in total.
Reason:Changed code type from text to php.
I'm not a huge fan of AJAX and didn't use much but this technology (AJAX) is definitely what you need. A few good AJAX libraries are scriptaculous (i think) and prototype! Look them up on the web.
crazycoders wrote:I'm not a huge fan of AJAX and didn't use much but this technology (AJAX) is definitely what you need. A few good AJAX libraries are scriptaculous (i think) and prototype! Look them up on the web.
At first sight your AJAX code looks right. I didn't test it. But given the fact that the meta refresh also didn't work, I guess you are running in a caching problem... your browser just cache the result and because of this do not update the information. You have either send cache-control headers or when calling the URL, use a random parameter name and/or value, which PHP file will ignore, but the browser will think that it loads different page.
Darhazer wrote:At first sight your AJAX code looks right. I didn't test it. But given the fact that the meta refresh also didn't work, I guess you are running in a caching problem... your browser just cache the result and because of this do not update the information. You have either send cache-control headers or when calling the URL, use a random parameter name and/or value, which PHP file will ignore, but the browser will think that it loads different page.
Hope this will help you!
The meta refresh did work but like I said in the first post, it's very annoying with browsers like Internet Explorer.