tags:

views:

148

answers:

3

I have a PHP file that executes multiple sql scripts. I find that the first two scripts execute, but the last two never seem to complete. I tested the last two scripts individually in their own PHP files and the scripts worked, so I'm wondering if it's because of the number of exec() calls I'm making in my PHP script. Any suggestions? Here's the original script:

<?php
session_start();

$host = "localhost"; 
$user = "user"; 
$pass = "pass"; 
$database = "database"; 

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
mysql_select_db($database, $linkID) or die("Could not find database."); 


$query = sprintf("SELECT dbName FROM users where userName='%s'",
   $_SESSION['MM_Username']); 
$resultID = mysql_query($query) or die("Data not found."); 

list($dbName)= mysql_fetch_row($resultID);

exec("mysql -u user -ppassword ".$dbName." < ../scripts/makeTblFinalAnalysis.sql"); 
exec("mysql -u user -ppassword ".$dbName." < ../scripts/tblFinalDetailed.sql");
exec("mysql -u user -ppassword ".$dbName." < ../scripts/TblGridViews.sql");
exec("mysql -u user -ppassword ".$dbName." < ../scripts/makeTblGeo.sql");
header( 'Location: dashboards/dashboard.html' ) ;
exit;
?>
+3  A: 

Might you be running out of execution time? How long are the scripts taking to run?

Look up set_time_limit() in the PHP documentation; if they take a while to run you will want to raise the PHP execution timeout.

staticsan
off memory the default max_execution_time is about 60sec. So if your scripts are taking longer then that in total, that could be the problem
bumperbox
Ok, I added the set_time_limit and it still doesn't work. My scripts were running under 30 seconds though
+1  A: 

"but the last two never seem to complete." perhaps because of some locks on the first two scripts, the third script is deadlocked? you can try to execute these scripts all 4 of them together on your sql server to check if it executes successfully and changes are commited. Also posting the sql queries might help.

Numenor
A: 

Try to join all execs into shell script and run it. Then try to run it in background. But then PHP will not wait for script return.

exec("nohup sqlRoutines.sh > /dev/null 2>&1 &");

this will detach script from calling process and redirect stdout and stderr to nowhere (or to logfile if you want...).

Jet
I'd like to try this. Sorry to ask, but I'm not very familiar with shell scripts. What would the command need to be to run the 4 exec commands in the shell script?