0

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?

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not exactly sure what you're asking (I lost your train of thought)... but if you are trying to get your scripts to run forever or at least as long as your host allows you(?) without using cronjob, then you can use the set_time_limit($seconds) function at the top of your script to set the max execution time allowed by your host. At the end of the script, have it sleep($seconds) and then call exec() to execute itself again. Be sure to factor in the execution time, or you will miss the trigger.

If your sql connections aren't working, then you're going to need to figure out what is going on there. If you post some of your code, we can help you debug. mysql_connect and mysql_pconnect for persistent connections are the commands you will be using to establish connectivity with your database.

1 Comment

i just figured out the error i am getting with the mysql_connect() command.. it shows the error no: 2002. this is being caused because the script is running in background i.e., not in the browser but using some other socket..!! am i getting it correct ..?
0

PHP scripts are not meant to be run as daemons (so many problems, of which the time limit is just the beginning) so you'll need a launcher.

pcntl_fork() is a simpler alternative to exec() if you're on a system that supports it.

As for your problem with exec(), the problem most likely has to do with the fact that the PHP interpreter you're executing has a different environment than the one that's running on your web server.

Try to execute phpinfo() on both environments to see what the actual differences are.

Though I must confess I can't see what would cause an "unable to connect to server" error; post the full error message, please. Can it be that a local firewall is blocking php.exe? Some other security measure, perhaps? Or could you be getting incorrect information regaring the MySQL server's hostname?

Try to run your exec'ed script from the command line. That'll get you close to what the web server is doing.

2 Comments

oki.. when i run in the terminal it shows the following error Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Applications/MAMP/bin/mamp/Test/Unnamed Site 4/test/db_connect.php on line 5 may be the socket is not accepted by my MySQL server.. How can i make it happen..? Am using Mac OS X Lion, and MAMP
Try connecting via TCP/IP instead of domain sockets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.