开发者

Create User with MySQLi Prepared Statement

开发者 https://www.devze.com 2023-04-01 05:37 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号