【发布时间】:2015-09-01 08:39:06
【问题描述】:
我在 IIS 和 windows 7 中运行 MySql + PHP。 这是我用来尝试创建新数据库 somedb 的 php 代码:
$cmd = escapeshellcmd('mysql -u root -p myPasswd -h localhost -Bse "create database somedb;" ');
$test = shell_exec("C:\\Program^ Files^ ^(x86)\\MySQL\\MySQL^ Server^ 5.6\\bin\\".$cmd);
var_dump($test);
echo "<br>============<br>";
print_r($test);
我放 var_dump 和 print_r 只是为了检查输出。 运行代码后,我有这个输出:
string 'C:\Program Ver 14.14 Distrib 5.6.23, for Win32 (x86)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Usage: C:\Program [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table'... (length=12085)
=========================================================================================
C:\Program Ver 14.14 Distrib 5.6.23, for Win32 (x86) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Usage: C:\Program [OPTIONS] [database] -?, --help Display this help and exit. -I, --help Synonym for -? --auto-rehash Enable automatic rehashing. One doesn't need to use 'rehash' to get table and field completion, but startup and reconnecting may take a longer time. Disable with --disable-auto-rehash. (Defaults to on; use --skip-auto-rehash to disable.) -A, --no-auto-rehash No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of mysql and disables rehashing on reconnect. --auto-vertical-output Automatically switch to vertical output mode if the result is wider than the terminal width. -B, --batch Don't use history file. Disable interactive behavior. (Enables --silent.) --bind-address=name IP address to bind to...
所以看起来 shell_exec 可以调用 bin 目录中的 mysql 但它没有创建数据库。这不是权限问题。我尝试在命令行中使用 sql 语句创建,效果很好。我已经尝试在没有 -h localhost 的情况下创建,或者将 -Bse 更改为 -e 或 --execute。 我已经尝试了这篇文章Create database in Shell Script - convert from PHP 的所有建议,但它对我不起作用。我错过了什么?
================================================ ==============================
仅用于更新,如果我在 Apache+win7 中运行,这是一个运行良好的代码:
echo shell_exec("C:\\xampp\\mysql\\bin\\mysql -u root -e \"CREATE DATABASE IF NOT EXISTS somedb\" ");
它创建数据库并且在浏览器中不显示任何内容。注意:不管 -u 和 root 之间的空格。
【问题讨论】: