[SOLVED] Mysql command via shell_exec, only the first one works

Issue

I need to run several sql command via shell_exec.
The first one works perfectly, but not the others.

I echoed the command ran by shell_exec, and those command works in terminal.

What am i doing wrong ?

$test1 = shell_exec('sudo mysql -u root -e "create database '.$name_db.';"');

$test2 = shell_exec('sudo mysql -u root -e "create user '.$user_db.'@localhost identified by '.$pass_db.';"');

$test3 = shell_exec('sudo mysql -u root -e "grant all privileges on '.$name_db.'.* to \''.$user_db.'\'@localhost;"');

$test4 = shell_exec('sudo mysql -u root -e "flush privileges";');

$test5 = shell_exec("sudo zcat $path_db_zip | mysql -u root $name_db");

All "$test*" print ‘NULL’.

Solution

A better way is to put it in a sh file and send it as a parameter.

cat  "mysql -u root -e create database `$1`;create user `$2`@`localhost identified by $3; grant all privileges on `$1` to `$2`@`localhost` with grant option; flush privileges;" > /addMysql.sh

then run shel_exec( sudo /addMysql.sh $db $username $password)

I didn’t test it but you can do it like this.

Answered By – Tayfun Yuksel

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published.