How to create database using php shell_exec and sq

2020-07-24 03:35发布

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);

标签: php mysql
2条回答
ゆ 、 Hurt°
2楼-- · 2020-07-24 04:38

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);
查看更多
冷血范
3楼-- · 2020-07-24 04:39

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

查看更多
登录 后发表回答