title: PHP/PDO - How do I change password of logged in user via PHP and PDO and refresh connection handle?
body: <p><strong>Scenario</strong></p>
  
  <p>I am trying to change the username of the current logged in mysql user via PDO and PHP and am unable to alter the password for the logged in user (in the mysql user table).</p>
  
  <p><em>I can change this only in my application users table, for which I want to use the same password.</em></p>
  
  <p><strong>Note</strong></p>
  
  <p>I am successfully able to change this via the mysql command line (Windows Command Prompt) but when I try to change this via my PHP script (changePassword) the old password remains.</p>
  
  <p><strong>Question</strong></p>
  
  <p>How can I alter the password in PHP/PDO?</p>
  
  <pre><code>    public function changePassword($username, $password, $password1, $password2)
      {
          $userPassExists = $this-&gt;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-&gt;dbh-&gt;prepare("UPDATE users set password = ':password' where username = ':username'");
                      $sql-&gt;execute(array(':password' =&gt; $this-&gt;createPwdHash($password1), ':username' =&gt; $username));
  
  
                      $sql = $this-&gt;dbh-&gt;prepare("UPDATE mysql.user SET Password=PASSWORD(':password') WHERE user=':username' AND Host='localhost';");
                      $sql-&gt;execute(array(':username', $username, ':password' =&gt; $password1));
  
                      $sql = $this-&gt;dbh-&gt;prepare("flush privileges");
                      $sql-&gt;execute();
  
                      $this-&gt;dbh = null;
                      $this-&gt;dbh = $this-&gt;getConnection($username, $password1);
                      var_dump($this-&gt;dbh);
              }
          }
          else
          {
                  echo "Incorrect Username or Password";
          }
      }
  </code></pre>
  
  <p>EDIT</p>
  
  <p>Following the syntax from the link below, I modified my function to the code below, but now it produces a warning.</p>
  
  <blockquote>
    <p>Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter
    number: number of bound variables does not match number of tokens in
    script.php on line 191</p>
  </blockquote>
  
  <p>Line 191 is </p>
  
  <pre><code> $sql = $this-&gt;dbh-&gt;prepare("set password for :username@'localhost' = PASSWORD(:password)");
  </code></pre>
  
  <p><strong>Link</strong>
  <a href="https://stackoverflow.com/a/22721480/1691103">https://stackoverflow.com/a/22721480/1691103</a></p>
  
  <pre><code>    public function changePassword($username, $password, $password1, $password2)
      {
          $userPassExists = $this-&gt;confirmUserNamePassword($username, $password);
          if ($userPassExists)
          {
              if ($password1 == '' or $password2 == '')
              {
                  echo "Complete all entries";
              }
              else if ($password1 != $password2)
              {
                  echo "New Passwords must match";
              }
              else
              {
                      $this-&gt;resetPassword($username, $password1);
                      $this-&gt;dbh = null;
                      $this-&gt;dbh = $this-&gt;getConnection($username, $password1);
                      var_dump($this-&gt;dbh);
              }
          }
          else
          {
                  echo "Incorrect Username or Password";
          }
      }
  
  
      public function resetPassword($username, $password)
      {
              $sql = $this-&gt;dbh-&gt;prepare("UPDATE users set password = :password where username = :username");
              $sql-&gt;execute(array(':password' =&gt; $this-&gt;createPwdHash($password), ':username' =&gt; $username));
  
              $sql = $this-&gt;dbh-&gt;prepare("set password for :username@'localhost' = PASSWORD(:password)");
              $sql-&gt;execute(array(':username', $username, ':password' =&gt; $password));
      }
  </code></pre>