jayesh vekariya
jayesh vekariya

Reputation: 117

How to change mysql user password?

Hi I am change mysql user password using bellow script but password does not change.

$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);          
}

Using this script password does not change and no error display.

Upvotes: 0

Views: 250

Answers (3)

Adrian Cid Almaguer
Adrian Cid Almaguer

Reputation: 7791

Try this, this can also help for sql injection.

$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";
}

Upvotes: 1

SHINERAJ ARATHIL
SHINERAJ ARATHIL

Reputation: 476

Just try this query

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

Upvotes: 0

anand kulkarni
anand kulkarni

Reputation: 156

try this query may this will help you

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

Upvotes: 0

Related Questions