PHP mysqli_change_user() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_change_user() switches the MySQL user (and optional default database) on an already open connection.

Object-oriented style uses $mysqli->change_user($username, $password, $database). A successful change resets session state like a fresh login. If the new login fails, the previous authentication stays in place.

Syntax

Call mysqli_change_user() with the values you need. Start with this line.

PHP
mysqli_change_user($mysqli, "demo", "secret", "shop");
CallWhat it does
mysqli_change_user($mysqli, "demo", "secret", "shop")Switches to user demo and selects the shop database.
mysqli_change_user($mysqli, "demo", "secret", null)Switches user without selecting a default database.
$mysqli->change_user("demo", "secret", "shop")Object-oriented form of the same call.

A successful change_user rolls back open transactions, drops temporary tables and clears session variables. Treat it like starting a new session on the same socket.

Library declarations
PHP
mysqli_change_user(mysqli $mysql, string $username, string $password, ?string $database): bool

Parameters

ParameterDescription
mysqlRequired. The open mysqli connection.
usernameRequired. The MySQL user name to switch to.
passwordRequired. The password for that user.
databaseRequired (may be null). Default database to select, or null for none.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Switch User on an Open Connection

Change to another account and print the default database. Sample output when connected to a demo database.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "test");

mysqli_change_user($mysqli, "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT DATABASE()");
$row = mysqli_fetch_row($result);
echo "Default database: ", $row[0], PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. Connect first as the demo user with database test.
  2. mysqli_change_user() switches credentials and selects shop.
  3. SELECT DATABASE() confirms the new default database.
  4. Sample output shows shop after the switch.

Example 2 — Session Variables Are Cleared

After change_user, earlier session variables are gone.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "test");
$mysqli->query("SET @flag := 1");

$mysqli->change_user("demo", "secret", "shop");
$result = $mysqli->query("SELECT @flag AS flag");
$row = $result->fetch_assoc();
echo "flag=", var_export($row["flag"], true), PHP_EOL;

$mysqli->close();

How It Works

  1. A user variable @flag is set to 1.
  2. change_user() resets the session like a new login.
  3. Selecting @flag now returns NULL.
  4. This shows why change_user is stronger than only picking a new database.

Example 3 — Failed Login Keeps the Old User

If the new password is wrong, the previous login remains.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");

$ok = mysqli_change_user($mysqli, "demo", "wrong-password", "shop");
echo $ok ? "switched" : "kept previous login", PHP_EOL;
echo "error: ", mysqli_error($mysqli), PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. A good connection already exists.
  2. change_user() is called with a bad password.
  3. The function returns false and the old session stays active.
  4. mysqli_error() explains the access denial.

Frequently Asked Questions

It re-authenticates an open connection as another MySQL user and optionally selects a new default database without closing the TCP connection first.
A successful change_user rolls them back and clears temporary tables and session state, as if you logged in again.
Yes. Passing null (or an empty string) opens the session with no default database selected.
The previous user stays logged in. Check the return value and mysqli_error() before assuming the switch worked.
When you already have a connection and want to switch identity without calling close and connect again. For most apps, separate connections per role are clearer.

Next: mysqli_character_set_name()

mysqli_character_set_name() returns the current character set name used by the MySQLi conn.

mysqli_character_set_name() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful