Fatal error: Uncaught mysqli_sql_exception: Unknown database 'bsmn'
When you import a database dump into phpMyAdmin and immediately encounter Fatal error: Uncaught mysqli_sql_exception: Unknown database 'bsmn', it usually means the database exists in your file system but is invisible to your PHP application's connection string. The error trace points directly to line 8 of your connection.php file, where the $dbname variable is passed to mysqli_connect. In this specific scenario, you are passing 'bsmn' as the fourth argument. MySQL treats this as an instruction to connect to that database immediately upon establishing the session. If the database does not exist at that exact moment, the connection fails before you can run any SQL commands to create it.
The root cause here is a timing mismatch between your manual import process and your application's initialization logic. You mentioned importing the DB via phpMyAdmin. While this action creates the tables inside the bsmn database, it does not guarantee that the database object itself exists if the dump script failed partway through or if you selected a different target database during the import wizard. However, even if the import was successful, simply selecting the database in the UI doesn't automatically update the PHP configuration array unless you explicitly created it first via SQL.
Consider the mechanics of mysqli_connect. The function signature is mysqli_connect(host, user, password, database). When a database name is provided as the fourth argument, MySQL attempts to switch to that database immediately. If bsmn does not exist in the server's data directory, or if it was created but the connection pool hasn't refreshed, the exception is thrown instantly. The code snippet provided:
if(!$con = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname))
{
die("failed to connect!");
}
will fail if $dbname refers to a non-existent schema. To resolve this, you must ensure the database is created before attempting the connection, or adjust the connection logic to handle the missing database gracefully.
The most robust approach when working with local environments like XAMPP is to create the database programmatically before the application tries to connect to it. You can modify your connection.php to attempt a drop and create sequence, though this requires care to avoid syntax errors if the connection fails entirely. A safer pattern for development environments involves creating a setup script that runs once to ensure the schema exists.
Create a file named setup_db.php in your project root:
<?php
// Ensure we are connecting without specifying a database initially
// so we can create it if it doesn't exist.
$con = mysqli_connect('localhost', 'root', '');
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
// Check if the database exists and create it if missing
$sql = "CREATE DATABASE IF NOT EXISTS bsmn";
if (mysqli_query($con, $sql)) {
echo "Database 'bsmn' created or already exists.\n";
// Now switch to the new database
$sql2 = "USE bsmn";
mysqli_select_db($con, "bsmn");
} else {
echo "Error creating database: " . mysqli_error($con);
}
mysqli_close($con);
?>
Run this script via your browser (http://localhost/bsmn/setup_db.php) once to ensure the bsmn schema is present in the MySQL server. After running it, refresh your main application. If the import was successful and the database now exists, your original connection.php code will work as intended because $dbname will resolve successfully.
If you are using Laravel or a similar framework where database configuration is centralized, ensure that your .env file matches the actual state of the database server. Sometimes developers assume the database name in the config matches the imported dump, but if the import failed silently or targeted a different database (e.g., bsmn_dev instead of bsmn), the mismatch occurs. Always verify the existence of the database on the server side using a terminal command like mysql -u root -e "SHOW DATABASES LIKE 'bsmn'".
Finally, consider the trade-off between strict error reporting and graceful fallbacks. In production, you might want to fail fast if the database is missing rather than trying to create it dynamically, which could expose sensitive creation logic or permissions issues. However, in a local XAMPP environment, the flexibility to create databases on-the-fly via a setup script is often more convenient than manually recreating them every time the dump changes.