English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_change_user() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_change_user() function is used to change the user of a specified database connection and set the current database.

Definition and usage

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

Syntax

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

Parameter

Serial numberParameters and descriptions
1

con(Required)

This is an object representing the connection with the MySQL Server.

2

user(Optional)

This is the name of the MySQL user you need to change.

3

password(Optional)

This is the password of the specified MySQL user

3

database(Optional)

indicating the name of the database that needs to be changed. If NULL is passed as the value for this parameter, this function only changes the user without selecting the database.

Return value

The mysqli_change_user() function returns a boolean value, which istrue;otherwisefalse.

PHP version

This function was originally introduced in PHP version5introduced and can be used in all higher versions.

Online Example

The following examples demonstratemysqli_change_user()Function usage (procedural style)-

<?php
   //Establish Connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   $res = mysqli_change_user($con, 	"w3codebox",	"abc123",	"mydb");
   if($res){
      print("User change successful");
   }
      print("Sorry, unable to change user");
   }
   //Close Connection
   mysqli_close($con);
?>

Output Result

User change successful

Online Example

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

<?php
   $host = "localhost";
   $username = 	"root";
   $passwd = "password";
   $dbname = "mydb";
   //Establish Connection
   $con = new mysqli($host, $username, $passwd, $dbname);
   $res = $con-> change_user("w3codebox",	"abc123",	"mydb");
   if($res){
      print("User change successful");
   }
      print("Sorry, unable to change user");
   }
   //Close Connection
   $res = $con -> close();
?>

Output Result

User change successful

Online Example

You can verify the database name after the change as follows:

//Establish Connection
$con = mysqli_connect("localhost", "root", "password", "mydb");
//Change Database
$res = mysqli_change_user($con, 	"w3codebox",	"abc123",	"mydb");
$list = mysqli_query($con, 	"SELECT DATABASE()");
if($list) {
    $row = mysqli_fetch_row($list);
    print("Current database: 	" . $row[0]);
}
//Close Connection
mysqli_close($con);
?>

Output Result

Current database: 	mydb

Online Example

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

PHP MySQLi Reference Manual