使用MySQLi预准备语句创建用户 [英] Create User with MySQLi Prepared Statement
问题描述
我有一个使用PHP和MySQLi并带有预准备语句的脚本.目的是在MySQL服务器上创建一个新用户,但是准备该语句失败,没有进一步的原因.
I have a script using PHP and MySQLi with prepared statements. The purpose is to create a new user on a MySQL server, however preparing the statement fails with no further information as to why.
$query = 'CREATE USER ?@`10.1.1.%` IDENTIFIED BY ?;';
if ($stmt = $newdb->prepare($query)) {
$stmt->bind_param('ss', $db_username, $db_password);
if ($stmt->execute()) {
// Database user created successfully
} else {
die(errorJSON('db', 'create', 22));
}
$stmt->close();
} else {
die(errorJSON('db', 'create', 3));
}
任何想法为什么执行此语句都会失败?
Any ideas why perparing this statement would fail?
谢谢.
推荐答案
在测试查询的成功/失败时,应打印$stmt->error
(和可选的$stmt->errno
).例如:
You should print $stmt->error
(and optionally $stmt->errno
) when testing the success/failure of your queries. For instance:
if ($stmt->execute()) {
// Database user created successfully
} else {
errorJSON('db', 'create', 22)
die($stmt->error);
}
以上是一个非常糟糕的例子-可能我弄错了错误报告本身的逻辑-但这是您检索错误并打印/记录错误的方式.
The above is quite a bad example - likely I've messed up the logic of your error reporting itself - but that is how you would retrieve the error and print/log it.
这篇关于使用MySQLi预准备语句创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!