MySQLi Tutorial MySQLi References

mysqli change_user() Method



The mysqli::change_user() / mysqli_change_user() function changes the user of the specified database connection and sets the current database.

In order to successfully change users, a valid username and password parameters must be provided and that user must have sufficient permissions to access the desired database. If for any reason authorization fails, the current user authentication will remain.

Syntax

//Object-oriented style
public mysqli::change_user(username, password, database)

//Procedural style
mysqli_change_user(mysql, username, password, database)

Parameters

mysql Required. For procedural style only: Specify a mysqli object returned by mysqli_connect() or mysqli_init().
username Required. Specify the MySQL user name.
password Required. Specify the MySQL password.
database Required. Specify the database to change to. If desired, the null value may be passed resulting in only changing the user and not selecting a database. To select a database in this case mysqli_select_db() function can be used.

Return Value

Returns true on success or false on failure.

Example: Object-oriented style

The example below shows the usage of mysqli::change_user() method.

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//setting variable a
$mysqli->query("SET @a:=1");

//resetting all, changing user 
//and selecting a new database
$mysqli->change_user("myUser", "myPassword", "myDb");

if ($result = $mysqli->query("SELECT DATABASE()")) {
  $row = $result->fetch_row();
  printf("Default database: %s\n", $row[0]);
  $result->close();
}

if ($result = $mysqli->query("SELECT @a")) {
  $row = $result->fetch_row();
  if ($row[0] === NULL) {
    printf("Value of variable a is NULL\n");
  }
  $result->close();
}

//closing the connection
$mysqli->close();
?>

The output of the above code will be similar to:

Default database: myDb
Value of variable a is NULL

Example: Procedural style

The example below shows the usage of mysqli_change_user() function.

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//setting variable a
mysqli_query($mysqli, "SET @a:=1");

//resetting all, changing user 
//and selecting a new database
mysqli_change_user($mysqli, "myUser", "myPassword", "myDb");

if ($result = mysqli_query($mysqli, "SELECT DATABASE()")) {
  $row = mysqli_fetch_row($result);
  printf("Default database: %s\n", $row[0]);
  mysqli_free_result($result);
}

if ($result = mysqli_query($mysqli, "SELECT @a")) {
  $row = mysqli_fetch_row($result);
  if ($row[0] === NULL) {
    printf("Value of variable a is NULL\n");
  }
  mysqli_free_result($result);
}

//closing the connection
mysqli_close($mysqli);
?>

The output of the above code will be similar to:

Default database: myDb
Value of variable a is NULL

❮ MySQLi Functions Reference