Create User with MySQLi Prepared Statement

橙三吉。 提交于 2019-12-22 17:40:09

问题


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?

Thank You.


回答1:


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.



来源:https://stackoverflow.com/questions/7258035/create-user-with-mysqli-prepared-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!