• PHP Video Tutorials

PHP mysqli_change_user() Function



Definition and Usage

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

Syntax

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

Parameters

Sr.No Parameter & 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 Values

The PHP mysqli_change_user() function returns a boolean value which is true if the database changed of successfully and false if not.

PHP Version

This function was first introduced in PHP Version 5 and works works in all the later versions.

Example

Following example demonstrates the usage of the mysqli_change_user() function (in procedural style) −

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

   $res = mysqli_change_user($con, "Tutorialspoint", "abc123", "mydb");

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

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

This will produce following result −

User changed successfully

Example

In object oriented style the syntax of this function is $con->change_user(); Following is the example of this function in 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("Tutorialspoint", "abc123", "mydb");

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

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

?>

This will produce following result −

User changed successfully

Example

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

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

//Changing the database
$res = mysqli_change_user($con, "Tutorialspoint", "abc123", "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);
?>

This will produce following result −

Current Database: mydb

Example

<?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", "abc123", "sampledb"); 
   mysqli_close($connection);
?>
php_function_reference.htm
Advertisements