checking database

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!

Moderator: General Moderators

shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

checking database

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.)
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Using an if or some other conditional would seem logical.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post 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';
   }

}
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

SHOW TABLE STATUS might be useful
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

trying to do reverse ajax
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Err. Ok. Why would you want to do this? I still see no reason for it.
ocpamit
Forum Newbie
Posts: 7
Joined: Fri Jul 13, 2007 6:53 am
Location: Punjab, India

Post 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.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

logging system means checking the database at pre-defined regular interval?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You could just make a table to store when a table was last updated and check against that date.
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post 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?
Post Reply