I have a script using PHP and MySQLi with prepared statements. Th开发者_Python百科e 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?
Thank You.
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.
精彩评论