Page 1 of 2

checking database

Posted: Fri Jul 13, 2007 6:35 am
by shivam0101
How can i check whether any changes (update, deleted) has been made?

I have an infinite loop, inside this loop i should be able to keep on checking the database

some what like this,

Code: Select all

while(1)
{
  checkdatabase
  if(res)
  {

 }
else
{

}


}
Thanks

Posted: Fri Jul 13, 2007 6:44 am
by feyd
Unless you store the previous pass, there's no way for you to compare outside of reading the log (if it's enabled.)

Posted: Fri Jul 13, 2007 6:58 am
by shivam0101

Code: Select all

while(1)
{
   $num=0;
   $query=mysql_query("SELECT * FROM online ORDER BY userid DESC");
   $num=mysql_num_rows($query);
}
i stored in $num, how do i check the next time

Posted: Fri Jul 13, 2007 7:18 am
by feyd
Using an if or some other conditional would seem logical.

Posted: Fri Jul 13, 2007 7:41 am
by shivam0101
how do i compare, suppose $num=6, in the next loop it does not change and after that it changes to 8

Code: Select all

while(1) 
{ 
   $num=0; 
   $query=mysql_query("SELECT * FROM online ORDER BY userid DESC"); 
   $num=mysql_num_rows($query);
  
   if($num==)//what to compare? 
   {
       echo 'Changed';
   }
   else
   {
      echo 'Not changed';
   }

}

Posted: Fri Jul 13, 2007 8:25 am
by Gente
SHOW TABLE STATUS might be useful

Posted: Fri Jul 13, 2007 9:05 am
by onion2k
Checking the number of rows is only going to let you know about inserts and deletes, updates won't show up.

What are you trying to do anyway? I can't think of any reason you'd want a script running forever doing something when any row changes.

Posted: Fri Jul 13, 2007 12:44 pm
by shivam0101
trying to do reverse ajax

Posted: Fri Jul 13, 2007 2:14 pm
by onion2k
Err. Ok. Why would you want to do this? I still see no reason for it.

Posted: Sat Jul 14, 2007 1:38 am
by ocpamit
as Gente suggested use SHOW TABLE STATUS.

SELECT ALL TABLES FROM THE DATABASE

IN A LOOP RUN ANOTHER QUERY TO SHOW TABLE STATUS

CHECK for UPDATE_TIME

MATCH WITH CURRENT TIME

i think it should solve the problem.

Posted: Sat Jul 14, 2007 5:17 am
by shivam0101
Err. Ok. Why would you want to do this? I still see no reason for it.
I searched google, saw information of doing it. It says to create an infinite loop and inside it put the query and flush the results. If you know a better way please let me know.

Posted: Sat Jul 14, 2007 7:19 am
by feyd
What's the reason behind wanting to infinitely query your database? I think it would serve better to use the logging system built into MySQL.

Posted: Sat Jul 14, 2007 9:44 am
by shivam0101
logging system means checking the database at pre-defined regular interval?

Posted: Sat Jul 14, 2007 2:41 pm
by superdezign
You could just make a table to store when a table was last updated and check against that date.

Posted: Sat Jul 14, 2007 2:56 pm
by smudge
You said you were doing this for ajax. I presume that your goal is to let a client know immediately that the database has changed. Instead of running a loop infinitely, try using ajax to poll the server every 2 sec or so, and the server then checks for updates. If there is, it sends the updated results. Is that what you're looking for?