3

I am using MAMP pro latest version Mac OS X 10.10

FROM cli I can run the following and it works:

exec("mysql --host=$db_host --user=$db_user --password=$db_password $db_name < $sql_file"); 

FROM apache/php it doesn't work and cannot find the executable.

It DOES work from apache/php if I do the following:

exec("/Applications/MAMP/Library/bin/mysql --host=$db_host --user=$db_user --password=$db_password $db_name < $sql_file"); 

What do I need to change to get this to work? It seems for some reason the environment variables aren't available when running from apache/php.

SaidbakR
  • 13,303
  • 20
  • 101
  • 195
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
  • Didn't you solve your problem? In a cli environment you have a $PATH environment variable that is a list of directories to check for executables. In many contexts this is not available and you have to explicitly include the path to the executable. Maybe you could use `putenv` to set the PATH variable, but I don't see what that solves – Dan Apr 15 '15 at 19:50
  • which mysql is `/Applications/MAMP/Library/bin/mysql`. The odd part is it works fine from cli but not apache. – Chris Muench Apr 15 '15 at 19:57
  • When I change to system instead of exec I get no output – Chris Muench Apr 15 '15 at 19:57
  • Your solution is what I have always seen as the acceptable fix for this issue. Why is it not acceptable? – Dan Apr 15 '15 at 20:05
  • I want to be able to deploy this in multiple environments and have mysql be found based on path. MAMP is just a development environment. I have been reading and it appears to be an apache environment issue but I cannot figure out how to set variables for apache. (modify $PATH) – Chris Muench Apr 15 '15 at 20:19
  • Does `exec('which mysql')` return `/Applications/MAMP/Library/bin/mysql` when run from apache/php? – Dan Apr 15 '15 at 20:42
  • It is empty (no result) echo "START"; system('which mysql'); echo "END"; die(); – Chris Muench Apr 15 '15 at 20:47

4 Answers4

2

I solved this by finding out the the environment path was /usr/bin:/bin:/usr/sbin:/sbin

I find this via phpinfo. I then symlinked mysql and mysqldump into /usr/bin and that solved my problem. I was I knew how to add paths to php; but this was good enough for what I needed.

Chris Muench
  • 17,444
  • 70
  • 209
  • 362
1

Similiar question asked here. PHP is probably invoking diferent shell. You can check which shell is used with this command:

php -r 'echo shell_exec("echo $0");'
Community
  • 1
  • 1
dafyk
  • 1,042
  • 12
  • 24
1

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!

Community
  • 1
  • 1
smcjones
  • 5,490
  • 1
  • 23
  • 39
  • Thanks for the info. I used multi_query and had issues with it before. (http://stackoverflow.com/questions/6102968/php-mysqli-multi-query-asynchronous). I like going straight to mysql as I know that will have the best results. – Chris Muench Apr 18 '15 at 18:07
  • Fair enough. Your question did not make mention of `store_result` which does help quite a bit. If you're looking for speed it may be worth it to go through the extra effort of configuration for option one so that you go directly through mysqli. If you're looking for ultimate portability however, I think PDO transactions may help to alleviate your concerns. – smcjones Apr 18 '15 at 20:51
-3

see "disable_functions" in php.ini or try function system

user3252197
  • 79
  • 1
  • 3
  • the exec command works if I give it the full path; but i need it to work without giving the full path as stated in question. – Chris Muench Apr 15 '15 at 19:48