I have two possible solutions for you. You can tweak your program or you can switch to a PHP approach.
Bash approach
OSX, from my somewhat limited experience, appreciates a full path for MySQL. I may not have the full story here, so if you test this command in bash straight from the command line and it works, then your best bet may be to insert the code as a bash script which you can execute.
//In your web scripts:
exec("doMySQL $db_host, $db_user, $dp_pass, $db_name $db_file");
//In mysql.php
!/bin/bash
mysql --host=$1 --user=$2 --password=$3 $4 < $5
Make sure you make your bash script executable.
If that doesn't work (I need to put the full path in my she'll be default for OSX), you may need to make sure you have a session environment for the owner of PHP. Or, you can place the full path in a settings file and change the path when you change environments. Personally I find this tedious and just one extra step that will lead to more effort in the long run, which leads to my next point.
PHP Solution
PHP would be my preferred solution because for little extra effort it is guaranteed to work anywhere PHP works, including Windows. So long as PHP can find MySQL, you will be all set. From my experience as a user and a developer, if you plan on having anyone else install this on their system, this will probably save you more time than writing instructions for each use case and helping people debug, and could certainly save that person time.
Utilizing a combination of file_get_contents()
and mysqli::multi_query()
should be nice and platform agnostic. WARNING: This has not been tested, nor is this "production quality". This is merely an example.
$file = file_get_contents($sql_file);
//You may want to take this opportunity to guard against unwanted
// code. For example, if you don't want DELETE syntax. Again, ignore
// if you feel good about your file not being tampered with.
$db = new mysqli($db_host, $db_user, $db_pass, $db_name);
// Now execute the query. Almost verbatim from PHP's documentation:
/* execute multi query */
if ($db->multi_query($file)) {
do {
/* store first result set */
if ($result = $db->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}
Alternatively, you could look at PDO and transactions.
try {
$dbh = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2',
array(PDO::ATTR_PERSISTENT => true));
echo "Connected\n";
} catch (Exception $e) {
die("Unable to connect: " . $e->getMessage());
}
try {
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->beginTransaction();
foreach(explode(";",$file) as $value) {
$dbh->exec($value);
}
$dbh->commit();
} catch (Exception $e) {
$dbh->rollBack();
echo "Failed: " . $e->getMessage();
}
Hope this helps!