Mysql - PDO Error - Invalid catalog name: 1046 No database selected

Mysql - PDO Error - Invalid catalog name: 1046 No database selected

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding the "Invalid catalog name" PDO Error on Shared Hosting

The error message SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected is a specific MySQL protocol response indicating that the server received a query targeting a non-existent schema or, more commonly in this context, that it was asked to operate on a database that wasn't explicitly selected. While local development environments often mask these issues due to relaxed permission sets, shared hosting providers like cPanel enforce stricter isolation and security policies that make this behavior visible immediately upon deployment.

The root cause lies in how the MySQL client library interprets the connection string versus how the server handles subsequent queries. In your specific case, the connection DSN (Data Source Name) includes dbname=Mydatabase. While modern PDO drivers often allow executing raw SQL against a database without an explicit USE statement if it is part of the connection string, shared hosting environments can behave differently depending on the underlying MySQL server version and configuration.

When you execute $dbh->query('SELECT * FROM table'), the PHP PDO extension sends this to the MySQL server. If the connection string's database name does not match the current session context expected by the specific MySQL user permissions on that server, or if the driver fails to implicitly set the catalog before executing the statement, the server returns error 1046. This is distinct from a "table not found" error; the table exists in your local environment, but the server cannot find it because it doesn't know which database to look inside.

To resolve this on shared hosting, you must ensure the database context is explicitly set before running any queries that do not qualify for implicit usage. The most robust approach involves calling PDO::exec() or a dedicated USE statement immediately after connecting but before executing your main query logic. This forces the driver to establish the session context regardless of how the server interprets the initial DSN.

Here is the corrected pattern:

$dbh = new PDO('mysql:host=serverName;dbname=Mydatabase', 'user', 'password');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Explicitly select the database to ensure session context
$dbh->exec("USE Mydatabase");

$res = $dbh->query('SELECT * FROM table');

Alternatively, you can structure your DSN to omit the database name entirely and rely solely on the USE statement. This method is often preferred in shared hosting environments because it ensures that the application logic explicitly manages the database context, avoiding potential conflicts with the server's default schema expectations or user-specific restrictions.

$dbh = new PDO('mysql:host=serverName', 'user', 'password');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$dbh->exec("USE Mydatabase"); // Set context here
$res = $dbh->query('SELECT * FROM table');

Another critical factor in shared hosting scenarios is the pdo_mysql configuration. Sometimes, the MYSQL_ATTR_INIT_COMMAND option needs to be set during connection initialization to ensure the database is selected immediately upon connection. However, relying on server-side flags like init_command can be brittle if your application logic changes or if you need to run queries in different databases within the same script.

If you are using a framework like Laravel, this issue is less common because the framework's database manager explicitly sets the connection prefix and handles the USE statement internally during migrations and seed runs. However, when dealing with raw PDO connections on a shared host, explicit context management is mandatory. Ensure that your .htaccess file does not inadvertently strip PHP session data or modify error reporting in a way that hides warnings before the fatal error occurs, though this specific error usually stems directly from the SQL state rather than server configuration.

Finally, verify that the database name in your code matches exactly with the one created in cPanel, including case sensitivity if you are on a Linux-based server. MySQL is case-sensitive for database names on Linux; MyDatabase and mydatabase are treated as different schemas. A mismatch here will trigger the "Invalid catalog name" error even if the table structure is perfect locally.

Our network