方案
我正在尝试通过PDO和PHP更改当前登录的mysql用户的用户名,并且无法更改登录用户的密码(在mysql用户表中)。
我只能在我的应用用户表中更改此功能,我希望使用相同的密码。
注意
我已成功通过mysql命令行(Windows命令提示符)进行更改,但当我尝试通过我的PHP脚本(changePassword)更改此密码时,旧密码仍然存在。
问题
如何更改PHP / PDO中的密码?
public function changePassword($username, $password, $password1, $password2)
{
$userPassExists = $this->confirmUserNamePassword($username, $password);
if ($userPassExists)
{
if ($password1 == '' or $password2 == '')
{
echo "Complete all entries";
}
else if ($password1 != $password2)
{
echo "New Passwords must match";
}
else
{
$sql = $this->dbh->prepare("UPDATE users set password = ':password' where username = ':username'");
$sql->execute(array(':password' => $this->createPwdHash($password1), ':username' => $username));
$sql = $this->dbh->prepare("UPDATE mysql.user SET Password=PASSWORD(':password') WHERE user=':username' AND Host='localhost';");
$sql->execute(array(':username', $username, ':password' => $password1));
$sql = $this->dbh->prepare("flush privileges");
$sql->execute();
$this->dbh = null;
$this->dbh = $this->getConnection($username, $password1);
var_dump($this->dbh);
}
}
else
{
echo "Incorrect Username or Password";
}
}
修改
按照以下链接的语法,我将我的函数修改为下面的代码,但现在它会产生警告。
警告:PDOStatement :: execute():SQLSTATE [HY093]:参数无效 number:绑定变量的数量与令牌的数量不匹配 第191行的script.php
第191行
$sql = $this->dbh->prepare("set password for :username@'localhost' = PASSWORD(:password)");
链接 https://stackoverflow.com/a/22721480/1691103
public function changePassword($username, $password, $password1, $password2)
{
$userPassExists = $this->confirmUserNamePassword($username, $password);
if ($userPassExists)
{
if ($password1 == '' or $password2 == '')
{
echo "Complete all entries";
}
else if ($password1 != $password2)
{
echo "New Passwords must match";
}
else
{
$this->resetPassword($username, $password1);
$this->dbh = null;
$this->dbh = $this->getConnection($username, $password1);
var_dump($this->dbh);
}
}
else
{
echo "Incorrect Username or Password";
}
}
public function resetPassword($username, $password)
{
$sql = $this->dbh->prepare("UPDATE users set password = :password where username = :username");
$sql->execute(array(':password' => $this->createPwdHash($password), ':username' => $username));
$sql = $this->dbh->prepare("set password for :username@'localhost' = PASSWORD(:password)");
$sql->execute(array(':username', $username, ':password' => $password));
}
答案 0 :(得分:1)
执行命令
$sql->execute(array(':username', $username, ':password' => $password1));
应该阅读
$sql->execute(array(':username' => $username, ':password' => $password1));