I have a simple script:

<?php

  $dbhost = 'localhost';
  $dbuser = 'root';
  $dbpass = 'password';
  $db = 'BPOJob';
  $file =dirname(__FILE__).'\\'.'job_create.sql';
  $mySQLDir='"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe"';     

  $cmd = $mySQLDir.' -h '.$dbhost.' --user='.$dbuser.' --password='.$dbpass.' < "'.$file.'"';

   exec($cmd,$out,$retval);
   echo "\n";
echo ($retval);

?>

The thing is when I run the above command in PHP.exe, it doesn't work; I got a 1 as a return value from exec.

But if I run the command line directly by calling

mysql.exe -h localhost --user=admin --password=password < job_create.sql

Then the .sql file can run.

Is there anything I miss?

Edit: Sorry! Wrong info. I have edited the question and it's still valid.

link|improve this question

54% accept rate
1  
Hm, shouldn't you exec $cmd instead of $mySQLDir? – Node May 2 '09 at 11:09
@Node: Hm, shouldn't you write answers to the answers section? :) – Tomalak May 2 '09 at 11:16
Hm, It feelt like a comment for me. :P But as demanded, posted as answer, now go and vote me down. ;) – Node May 2 '09 at 11:21
Shouldn't this question be posted in stackoverflow instead? – Vinko Vrsalovic May 2 '09 at 11:24
1  
this belongs on stackoverflow.com ... – Alnitak May 2 '09 at 11:33
show 2 more comments
feedback

3 Answers

Hm, shouldn't you exec $cmd instead of $mySQLDir?

EDIT: Try to echo $cmd maybe you could see an error.

link|improve this answer
feedback

The spaces in the command line are probably being treated as separators, so it's actually trying to run:

c:\program

with arguments:

files ...

Try enclosing the whole of the executable path in double-quotes as it's passed to exec(), so that the windows shell knows that it's supposed to be one word.

link|improve this answer
Good point. +1 – Tomalak May 2 '09 at 11:44
I thought it's already enclosed? $mySQLDir='"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe"'; – Graviton May 2 '09 at 11:52
so it is - I couldn't see the ' " " ' sequence the first time – Alnitak May 2 '09 at 12:20
feedback
up vote 1 down vote accepted

I found the solution: you need to explicitly enclosed the $cmd in quote:

 exec('"'.$cmd.'"',$out ,$retval);

This is the full script, for your reference:

<?php

  $dbhost = 'localhost';
  $dbuser = 'root';
  $dbpass = 'password';
  $db = 'BPOJob';
  $file =dirname(__FILE__).'\\'.'job_create.sql';
  $mySQLDir='"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe"';     

  $cmd = $mySQLDir.' -h '.$dbhost.' --user='.$dbuser.' --password='.$dbpass.' < "'.$file.'"';

   exec('"'.$cmd.'"',$out,$retval);
   echo "\n";
echo ($retval);

?>
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.