Sri Vishnu Totakura Sri Vishnu Totakura - 7 months ago 29
SQL Question

Running a PHP script containing MySQL Queries in background with out using cronjobs

I have a php script as "save.php" which contains sql queries, and need to run this script in the background i.e., not in the browser. For calling this script I am using the exec() command in file named "trigger.php".

now when

save.php
is called by
exec()
it runs normal when the mysql queries are not used., but when I put the mysql queries the queries alone doesn't work and rest of the script executes fine. As far as I am thinking the mysql_connect is not able to run.

I cannot use cronjob because my need is different, I use need to trigger the script and it should be running continuously, but not in intervals.

So, is there a way where I can create a MySqL Server connection in the file which needs to be running in the background?

Answer

I have managed to figure out the problem.

I'm running MAMP for PHP and MySQL. MySQL's socket is created in the MAMP's MySQL directory but when we call a PHP script using exec(), the script is being triggered from the shell/command prompt which checks for the MySQL socket in the usual directory, which is /var/mysql/mysql.sock. Which is why we get the following error.

Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

So, I created a directory under /var and added a symbolic link to the actual socket using

 sudo mkdir /var/mysql

 sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

Works very well now.

Thanks everyone for the suggestions!

Comments