PHP | mysqli_change_user() Function

PHP mysqli_change_user() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_change_user() Function.

The mysqli_change_user() function accepts a connection object, user name, password and, a database name as parameters, then changes the user and database in the given connection object to the specified user and database.

Syntax

Following below is the syntax to use this function -

mysqli_change_user($con, $user, $password, $database);


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

2

user(Optional)

This is a name of a MySQL user to which you need to change.

3

password(Optional)

This is a password of the specified MySQL user

3

database(Optional)

This represents the name of the database to which you need to change. If you pass NULL as a value to this parameter, this function just changes the user without selecting the database.


Return Value

This built-in PHP function returns a boolean value which is true if the database changed successfully and false if not.

PHP Version

This built-in function was first introduced in PHP version 5 and it works in all of the later versions.

Example1

The following example illustrates the usage of the PHP mysqli_change_user() function (in a procedural style) -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   $res = mysqli_change_user($con, "Webdesigntutorialz", "abc1234", "mydb");

   if($res){
      print("User changed successfully");
   }else{
      print("Sorry Couldn't change the user");
   }

   //Closing the connection
   mysqli_close($con);
?>

Output

When the above code is executed, it will produce the following result -

User changed successfully

Example2

In an object oriented style the syntax of this function is $con->change_user(); Following is the example of this function in an object oriented style $minus;

<?php
   $host = "localhost";
   $username  = "root";
   $passwd = "password";
   $dbname = "mydb";

   //Creating a connection
   $con = new mysqli($host, $username, $passwd, $dbname);

   $res = $con->change_user("Webdesigntutorialz", "abc1234", "mydb");

   if($res){
      print("User changed successfully");
   }else{
      print("Sorry couldn't change the user");
   }

   //Closing the connection
   $res = $con -> close();

?>

Output

When the above code is executed, it will produce the following result -

User changed successfully

Example3

You can verify the database name after the change as shown below -

//Creating a connection
$con = mysqli_connect("localhost", "root", "password", "mydb");

//Changing the database
$res = mysqli_change_user($con, "Webdesigntutorialz", "abc1234", "mydb");

$list = mysqli_query($con, "SELECT DATABASE()");

if($list) {
    $row = mysqli_fetch_row($list);
    print("Current Database: ". $row[0]);
}

//Closing the connection
mysqli_close($con);
?>

Output

When the above code is executed, it will produce the following result -

Current Database: mydb

Example4

Try the following example below -

<?php
   $connection = mysqli_connect("localhost","root","password","mydb");
   
   if (mysqli_connect_errno($connection)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }   
   mysqli_change_user($connection, "myuser", "abc1234", "sampledb"); 
   mysqli_close($connection);
?>


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the PHP mysqli_character_set_name() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.