我正在尝试创建一个简单的PHP脚本来为SQL用户创建和分配属性。如果没有使用参数,查询工作正常,否则失败。
工作正常
$conn = new mysqli('127.0.0.1', 'root', 'pass');
$sql = "CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'";
$stmt = $conn->prepare($sql);
$stmt->execute();
用户名和密码均为?
$conn = new mysqli('127.0.0.1', 'root', 'pass');
$sql = "CREATE USER '?'@'localhost' IDENTIFIED BY '?'";
$stmt = $conn->prepare($sql);
$uname = "carn";
$pass = "pass";
$stmt->bind_param('ss', $uname, $pass);
$stmt->execute();
用户名和密码均为?
$conn = new mysqli('127.0.0.1', 'root', 'pass');
$sql = 'CREATE USER "?"@"localhost" IDENTIFIED BY "?"';
$stmt = $conn->prepare($sql);
$uname = "carn";
$pass = "pass";
$stmt->bind_param('ss', $uname, $pass);
$stmt->execute();
语法错误,请检查文档
$conn = new mysqli('127.0.0.1', 'root', 'pass');
$sql = "CREATE USER ?@'localhost' IDENTIFIED BY ?";
$stmt = $conn->prepare($sql);
$uname = "carn";
$pass = "pass";
$stmt->bind_param('ss', $uname, $pass);
$stmt->execute();
语法错误,请检查文档
$conn = new mysqli('127.0.0.1', 'root', 'pass');
$sql = "CREATE USER `?`@'localhost' IDENTIFIED BY `?`";
$stmt = $conn->prepare($sql);
$uname = "carn";
$pass = "pass";
$stmt->bind_param('ss', $uname, $pass);
$stmt->execute();
我不知道从哪里开始。这可能是我使用引号的方式的一个问题我在这里看到了两个相关的问题,但没有一个正确回答。
答案 0 :(得分:1)
使用预准备语句,您只能使用有限的查询子集。创建未包含的用户。
您可以尝试INSERT到mysql.user
表,例如
INSERT INTO `mysql`.`user` (Host,User,Password)VALUES(?,?,password(?));
此外,动态创建的用户你做错了
答案 1 :(得分:1)
不要在?
附近放置单引号,bind_param()会处理它。
但是,还有一个问题是您无法在MySQL中使用CREATE USER
语句的占位符。
请参阅How to write "create user ?" with MySQL prepared statement了解相关方法。