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
$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
- Connect first as the demo user with database test.
- mysqli_change_user() switches credentials and selects shop.
- SELECT DATABASE() confirms the new default database.
- Sample output shows shop after the switch.

