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
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

 

 

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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?

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.