2

i am trying to create a database using shell_exec and mysql commands. I do not want to use php built-in mysql_query because of severals reasons. However, i cant get the below command to execute successful. Anyone can show me some light on what went wrong?

$test = shell_exec("mysql -u root -pmypassword create database db_hello;");

var_dump($test);
3
  • 5
    I'd really like to know what the "several reasons" are Commented Jun 7, 2012 at 23:13
  • shell_exec should be disabled, I don't think this is a good idea, I'd never do it. Commented Jun 7, 2012 at 23:21
  • 1
    @markus-tharkun along with exec(), passthru() and system() Commented Jun 7, 2012 at 23:24

2 Answers 2

8

The correct syntax is like this:

mysql -u [username] -p -e "create database somedb"

Or

mysql --user=user_name --password=your_password -e "SELECT 1 FROM information_schema.tables"

Reference: http://www.electrictoolbox.com/run-single-mysql-query-command-line/

$cmd = escapeshellcmd('mysql -u [username] -p -e "create database somedb"');
$test = shell_exec($cmd);

var_dump($test);
Sign up to request clarification or add additional context in comments.

3 Comments

the command is ok when i run it through mysql command line, however, it i run it through php it does not work
tried your code in php, it doesnt seems to work. return NULL.
Thank you all. Decided to use mysql_query instead.
0

The issue might be the path to mysql in your php script. For me was : /usr/local/bin/mysqld (osx 10)

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.