Jump to content

PHP Query - Auto Refresh using AJAX.


Recommended Posts

Hi Guys.

I have a script that runs a query on a SQL Database and returns results. The results in question will be updated very frequently (every 5 seconds) and I need a way of auto-updating the results shown on the PHP Page.

 

Here is my script...

<?php
$result1 = mysql_query("SELECT * FROM ajaxim_users", $openDB);
$num_rows1 = mysql_num_rows($result1);
?>
<?php
$result2 = mysql_query("SELECT * FROM ajaxim_users WHERE is_online = 1 AND admin = 1", $openDB);
$num_rows2 = mysql_num_rows($result2);
?>
<?php
$result3 = mysql_query("SELECT * FROM ajaxim_users WHERE is_online = 1 AND admin = 0", $openDB);
$num_rows3 = mysql_num_rows($result3);
?>
<?php
$result4 = mysql_query("SELECT * FROM ajaxim_users WHERE banned = 1", $openDB);
$num_rows4 = mysql_num_rows($result4);
?>
<?php
$result5 = mysql_query("SELECT * FROM ajaxim_chats", $openDB);
$num_rows5 = mysql_num_rows($result5);
?>
<strong>Members:</strong><br /><?echo"$num_rows1";?><hr />
<strong>Online Admins:</strong><br /><?echo"$num_rows2";?><hr />
<strong>Online Users:</strong><br /><?echo"$num_rows3";?><hr />
<strong>Banned Users:</strong><br /><?echo"$num_rows4";?><hr />
<strong>Chatrooms:</strong><br /><?echo"$num_rows5";?><hr />

 

I have tried for days to find a script that works. And not only do none of the google-found scripts not work, they are unreasonably long. All I want to do is auto-refresh the above script every 10 seconds.

 

I will be grateful for any help you can offer.

Link to comment
https://forums.phpfreaks.com/topic/139331-php-query-auto-refresh-using-ajax/
Share on other sites

Can you do it with meta tag

 

in <head> add <META HTTP-EQUIV=Refresh CONTENT='100; URL=index.php'>  give time in seconds here it is 100 and the url of file to refresh.

 

We used this way to refresh teh existing admin script to get the new records on our pages .

 

Thanks

 

 

You can also use setTimeout().

 

Also, your first 4 queries can probably be combined to make the process more efficient.

    (Creating 5 queries creates extra overhead.)

Also, you should not be selecting * especially if you are only looking for the num_rows.

    (Selecting more columns that you are using creates extra overhead.)

What if the

Can you do it with meta tag

 

in <head> add <META HTTP-EQUIV=Refresh CONTENT='100; URL=index.php'>  give time in seconds here it is 100 and the url of file to refresh.

 

We used this way to refresh teh existing admin script to get the new records on our pages .

 

Thanks

 

 

 

what if the file is an include in a div that has a header can you refresh the content of just the div?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.