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".
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():  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!