PDO connects to MySQL, then every query fails with No database selected
Your error
PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected1 more line it can showFix it fast
Most common cause, 1 of 3The DSN has no dbname element
Add dbname to the DSN, and set the charset in the same string.
$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=shop;charset=utf8mb4';
$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);Short answer
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected means the PDO connection opened with no default database, so the first query on a bare table name fails. A MySQL DSN must carry dbname=name. PDO silently skips a misspelled key such as database=, and an empty environment value leaves dbname blank. Correct the DSN or the variable behind it. PHP 8.2-8.6.
Which cause do you have?
Save this as check-dsn.php next to the app's config. It prints the DSN the code really builds (keep the password out of it) and asks the server which database the connection uses. NULL confirms the connection has none; the DSN string then shows which cause applies.
<?php
$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
var_dump($dsn, $pdo->query('SELECT DATABASE()')->fetchColumn());Then find the line you see in this list and open its fix:
- 1The DSN has no dbname element
The printed DSN looks like mysql:host=127.0.0.1;port=3306 and stops there - 2The database key is spelled differently, so PDO ignores it
The DSN does name the database, but as database=, db=, dbName= or dbname = with spaces - 3dbname comes from an environment value that is empty at run time
The printed DSN ends in dbname= with nothing after it, or a Laravel app shows an empty database value
What does the error look like?
PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selectedFatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
Cause 1 of 3
The DSN has no dbname element
You seeThe printed DSN looks like mysql:host=127.0.0.1;port=3306 and stops there
Check
SQLSTATE[3D000] with error 1046 is what PDO reports when the MySQL session has no default database. The PDO_MYSQL DSN takes the database from its dbname element; host and port only choose the server. Without dbname the login still succeeds, so the failure shows up only at the first query.
grep -rn "mysql:host" --include='*.php' src/ config/Fix
Add dbname to the DSN, and set the charset in the same string.
$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=shop;charset=utf8mb4';
$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);Verify
SELECT DATABASE() now returns the name.
var_dump($pdo->query('SELECT DATABASE()')->fetchColumn()); // string(4) "shop"Confirmed by php.net
Cause 2 of 3
The database key is spelled differently, so PDO ignores it
You seeThe DSN does name the database, but as database=, db=, dbName= or dbname = with spaces
Check
PDO's DSN parser matches each key against the names the driver expects with a case-sensitive comparison of the whole key. Anything else, including database=, dbName= or a key with a space before the equals sign, is skipped without a warning. The connection then opens with no default database and the 1046 error follows on the first query.
grep -rn "mysql:" --include='*.php' . | grep -v "dbname="Fix
Write the key exactly as dbname, in lower case, with no space around the equals sign.
// ignored by PDO: 'mysql:host=127.0.0.1;database=shop'
// ignored by PDO: 'mysql:host=127.0.0.1;dbName=shop'
$dsn = 'mysql:host=127.0.0.1;dbname=shop';Verify
The first-step script prints the database name instead of NULL.
php check-dsn.phpConfirmed by raw.githubusercontent.com
Cause 3 of 3
dbname comes from an environment value that is empty at run time
You seeThe printed DSN ends in dbname= with nothing after it, or a Laravel app shows an empty database value
Check
When the DSN is built from a variable, a missing or empty value produces dbname= with no name, and the connection opens without a default database. Laravel builds its DSN this way from config('database.connections.mysql.database') and only runs a USE statement when that value is not empty. After php artisan config:cache, Laravel no longer reads .env at all, so a value added to .env later stays empty until the cache is rebuilt.
php artisan config:show database
grep -n "^DB_DATABASE" .envFix
Set the variable where the process really reads it. In Laravel, set DB_DATABASE in .env, then clear and rebuild the configuration cache.
# .env
DB_DATABASE=shop
php artisan config:clear
php artisan config:cacheVerify
config:show lists the database name for the mysql connection, and the query that failed runs.
php artisan config:show database | grep -n "shop"Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Build the DSN in one place and fail at startup when the database name is empty, instead of at the first query.
- Log the DSN without the password at boot in staging, so a missing dbname is visible in the first line of the log.
- In Laravel, read env() only inside config files and rebuild the config cache on every deploy.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Spotless Laundry, EZ Estimater, TaxiDex.