PHP error guide
SQLSTATE 42S02 table does not exist: meaning and fix
Error summary
Run the correct migration and verify database selection and table-name case. The query names a table unavailable in the selected schema.
What it means
Run the correct migration and verify database selection and table-name case. The query names a table unavailable in the selected schema.
What the error means
This message means that the query names a table unavailable in the selected schema. The exact signature distinguishes sqlstate 42s02 table does not exist from a generic application failure.
Why PHP produces it
The engine or service reports “SQLSTATE 42S02 table does not exist” because its required precondition was not met. Run the correct migration and verify database selection and table-name case.
PHP version notes
The sqlstate 42s02 table does not exist wording here is based on PDO with MySQL/MariaDB; exact server wording varies; punctuation and exception class names can differ on older branches or vendor builds.
Most common causes
- The immediate input or configuration reaches the specific condition: the query names a table unavailable in the selected schema
- The code path assumes the prerequisite for sqlstate 42s02 table does not exist has already been satisfied.
- For sqlstate 42s02 table does not exist, development and production differ in version, extension, permissions, paths, or service configuration.
- An earlier operation returned an unchecked value that is consumed by the line reporting sqlstate 42s02 table does not exist.
Minimal examples
BAD — reproduces the problem
$pdo->query("SELECT * FROM order");
FIXED — safer pattern
$pdo->query("SELECT * FROM orders");
Step-by-step diagnosis
- Copy the complete “SQLSTATE[42S02]: Base table or view not found: 1146 Table 'app.orders' doesn't exist” text and retain the first application stack frame.
- Reproduce sqlstate 42s02 table does not exist in the same SAPI and environment listed for this page.
- Before changing sqlstate 42s02 table does not exist, inspect the preceding value or directive and verify its type, path, version, and permissions.
- Apply the narrow correction—run the correct migration and verify database selection and table-name case—then repeat the original request once.
Fixes
Correct the failing prerequisite
Run the correct migration and verify database selection and table-name case
$pdo->query("SELECT * FROM orders");
Fail explicitly at the boundary
Validate the condition before the operation that emits sqlstate 42s02 table does not exist, and log a safe diagnostic without credentials or full production paths.
Common mistakes when fixing it
- Suppressing sqlstate 42s02 table does not exist instead of correcting its upstream condition.
- Testing sqlstate 42s02 table does not exist only with the CLI binary when the failing request runs under FPM or Apache.
- Changing a global setting for sqlstate 42s02 table does not exist before confirming the site-specific effective configuration.
How to prevent it
- Add a focused test that exercises the boundary responsible for sqlstate 42s02 table does not exist.
- Keep runtime versions, extensions, configuration, and deploy artifacts affecting sqlstate 42s02 table does not exist reproducible.
- Validate external data and service return values before they can trigger sqlstate 42s02 table does not exist.
Web server / environment notes
fpm, cli, docker, linux. The failure occurs where the query names a table unavailable in the selected schema
Tags: fpm,cli,docker,linux