0

Referencing this post as well(How to create database using php shell_exec and sql command line)

I've followed the accepted answer in that post, but I am curious as to why the shell_exec is unable to run mysql code. It merely returns NULL.

The user already has sudo access, when i copy the following command line into the console log manually it works.

$cmd = escapeshellcmd('sudo mysql -u root -e "create database somedb"');
$test = shell_exec($cmd);

var_dump($test);

Edit 1: Updated [username], if root may cause some issues

5
  • 1
    Can you post the command you are trying to run? Commented Jul 19, 2019 at 3:58
  • 1
    please show us the $cmd Commented Jul 19, 2019 at 3:58
  • very likely you will need to use full path in the command Commented Jul 19, 2019 at 3:59
  • A better way to create a database would be to use mysqli or pdo to do so in PHP rather than using shell_exec. Commented Jul 19, 2019 at 3:59
  • sorry was a formatting issue, the cmd was hidden cause i had it inline with the triple ( ` ) Commented Jul 19, 2019 at 4:07

1 Answer 1

0

You will need to use the full path to mysql

Type whereis mysql to find it.

It will most likely be /usr/bin/mysql

Change your command to:

$cmd = escapeshellcmd('sudo /usr/bin/mysql -u [username] -e "create database somedb"');

You may need full path to sudo .. same principles will apply.

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

1 Comment

tried the suggested command but it didnt work, still returned null,but im sure the path is correct because when i run it in the console directly, it works.

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.