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;
?>
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.
"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.
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...).