如何更改mysql用户密码?

时间:2015-04-03 10:14:56

标签: php mysql sql database mysqli

您好我使用下面的脚本更改mysql用户密码,但密码不会更改。

$conn = mysqli_connect('host', 'root', 'password');
$dbUser = "username"; # same username as in your example
$dbPass = "new_password"; # new password

$queries = array(
  "USE mysql;", # switch to the 'mysql' database
  "SET PASSWORD FOR '$dbUser'@'localhost' = PASSWORD('$dbPass');"
);

foreach($queries as $query) {
    $rs = mysqli_query($conn, $query);          
}

使用此脚本密码不会更改,也不会显示错误。

3 个答案:

答案 0 :(得分:1)

试试这个,这也可以帮助sql注入。

$query = sprintf("UPDATE mysql.user SET Password = '%s' WHERE User='%s'",mysqli_real_escape_string($dbPass),mysql_real_escape_string($dbUser));
$result = mysqli_query($query);
if($result){
   echo "change success";
}

答案 1 :(得分:0)

尝试此查询可能会对您有所帮助

mysql_query("UPDATE mysql.user SET Password = PASSWORD('$password') WHERE User='$username'");

答案 2 :(得分:0)

试试这个查询

SET PASSWORD FOR 'bob'@'%.example.org' = PASSWORD('cleartext password');