Question

I have a program which makes changes to an MSSQL database. I wanted some actions to happen when these changes are made, like sending an e-mail. This program doesn't do any of those things and I also can't modify the program, so as my question says.. can I use PHP to keep checking for changes in the database and do those actions? I was thinking something like:

$originalDBContent = "SELECT events FROM evTable";

while(true){
  $checkContent = "SELECT events FROM evTable";
  while($originalDBContent == $checkContent) sleep(1);

  //command below if changes made to db
  mail(...);
  }

Will it work or any other programs suggested to accomplish this?

Was it helpful?

Solution

The use of triggers with sqlcmd should be done easily enough

http://technet.microsoft.com/en-us/library/ms170572.aspx

this would allow you to call the php command

Another way could be (im not sure of the exitinace in mssql) monitor the binary log change date, The binary log shows all modifications to data on a server. you could check the modified time on the file and keep track.

The last option requires you to have full control. But on insert you could run the script. This wouldnt fire anything assuming you change the data manually though, So all changes would need to be made though your application

The best option would be trigger + sqlcmd

More info and examples

http://dbalink.wordpress.com/2008/06/20/how-to-sql-server-trigger-101/

OTHER TIPS

no, you can't do that on PHP since if you do that

while(true){
  $checkContent = "SELECT events FROM evTable";
  while($originalDBContent == $checkContent) sleep(1);

  //command below if changes made to db
  mail(...);
  }

this will cause the browser or whoever accessing it got a timeout since it'll make the page keep loading.

the best thing you can do with PHP is doing a cron job but why did you need to check every time database changed ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top