Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement'

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement'

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

Stefan Bogdanescu

Founder & Senior Architect

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement'

The error message Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement' often appears when developers upgrade their MySQL server versions, specifically moving from 5.7 to 8.0. It is a confusing failure that halts execution immediately after the execute() call but before any data retrieval logic runs.

The root cause lies in how modern MySQL optimizes prepared statements differently than traditional queries. When you execute a simple statement like $get_emp_list->execute(), the server expects to determine the execution plan internally. In older versions, this worked fine even without an index on the selected columns if no WHERE clause or sorting was applied. However, MySQL 8.0 introduced stricter checks for prepared statements that lack a defined execution context. Specifically, if you are using functions or expressions in your SELECT list that prevent the optimizer from caching the result set plan efficiently, or if the statement structure is ambiguous without an index to anchor the read, the server throws this exception to force the developer to be explicit about data access patterns.

In the provided code snippet, the query SELECT id, name FROM calc appears harmless. The table schema shows a primary key on id, which should theoretically satisfy most indexing requirements. However, the presence of this error suggests a version mismatch or a specific configuration in MySQL 8.0 where the optimizer refuses to execute a prepared statement unless it can guarantee a stable plan.

To resolve this without upgrading the application stack or ignoring security best practices regarding prepared statements, you must ensure that the query is explicitly optimized for caching. One common workaround involves adding an ORDER BY clause that utilizes an existing index. Since your table has a primary key on id, modifying the query to return results in order might satisfy the optimizer:

$get_emp_list = $mysql->prepare("SELECT id, name FROM calc ORDER BY id");

This forces the storage engine to use the primary key index for both retrieval and sorting. If you do not need a specific order, you can add LIMIT 0 or ensure that no functions are applied to the selected columns that would break plan caching. In some MySQL 8.0 configurations, simply adding /*+ NO_CACHE_PLAN */ hint might be necessary if the optimizer gets stuck in a loop trying to cache a non-cacheable plan, though this is an advanced technique and generally not recommended for production without thorough testing.

Another potential pitfall involves the database engine type. Your schema defines the table as ENGINE=MyISAM. While MyISAM supports primary keys, InnoDB (the default in modern installations) handles prepared statements more robustly regarding index usage statistics. If possible, migrating to InnoDB and ensuring that the columns selected are indexed will eliminate this exception entirely.

If you are building an application using a framework like Laravel, you might encounter similar issues if raw SQL queries are injected into prepared statement contexts without proper optimization. The principle remains the same: modern database engines demand clarity on how data is accessed. If the query does not specify an index path, the engine may reject the prepared statement to prevent performance degradation from repeated plan recalculations.

For further reading on database optimization strategies and best practices for handling prepared statements across different MySQL versions, you can refer to resources available at https://laravelcompany.com.

Our network