0

Is it possible to, from within a PHP script, execute the same commands you could with the MySQL client?

I know I could theoretically call 'system' to invoke the mysql client installed on the system, but I am not sure how to avoid interactivity (I don't want a REPL/shell, I just want to fire a command). Is there a way to execute commands via the mysql client without going into the shell? In either case this approach seems a bit sketchy.

To clarify, when I say command I am referring to the follow: http://dev.mysql.com/doc/refman/5.0/en/mysql-commands.html

1
  • What command specifically do you want to execute? Commented Sep 30, 2010 at 19:42

2 Answers 2

1
while ($obj = mysql_fetch_object($res)) {
        $file = $path.$obj->base.".sql";
        $cmd = "rm -f ".$file;
        exec($cmd);
        $cmd = "nice -19 mysqldump -h".$host." -u".$login." -p".$pass." ".$obj->base." > ".$file;
        exec($cmd);
        $sql = "update save_mysql set last_daily=NOW() where base = '".$obj->base."'";
        mysql_query($sql);
}

Are you looking for something like this.

using exec function you can call mysql command`

Sign up to request clarification or add additional context in comments.

1 Comment

Could you indent your code 4 spaces? Select the chunk, and click the 1010 button. This will make it nicely formatted and easy to read.
0

Sounds hacky. There's Expect to handle "programming interactivity", i.e. a way to wait for the prompt to say particular things. I don't think exec will work, unless you can pass specific switches and run mysql in a non-interactive mode. Depending on what you're trying to do I'd say it best to try and figure out a SQL command you can send through the regular client libs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.