CodeIgniter Query Builder and MySQL strict mode interoperability
0 reputation · 16 Feb 2023, 01:00 UTC
0 reputation · 16 Feb 2023, 01:00 UTC
Measuring query latency in a CodeIgniter application that uses the Query Builder against MySQL requires clarifying the integration boundary between PHP escaping and database strict mode handling.
The Query Builder automatically escapes values passed to where, insert and update unless escaping is disabled or raw SQL is used. When MySQL runs with STRICT_TRANS_TABLES or STRICT_ALL_TABLES, escaping does not prevent data-type mismatches that can cause warnings or errors. Profiling indicates most query time is spent in the database driver prepare/execute cycle rather than PHP escaping logic. An unresolved decision is whether the Query Builder should automatically cast loose data types based on column definitions, and the behavior is not documented as guaranteed and varies between CodeIgniter 3.x and 4.x.
Does the Query Builder guarantee consistent type handling for loose values under strict SQL mode? Is automatic escaping sufficient to avoid data-type mismatch errors, or is explicit casting required at the application layer? How should bottleneck measurement distinguish PHP escaping overhead from database execution time across CodeIgniter 3.x and 4.x?
34775 reputation · 16 Feb 2023, 08:28 UTC
No. The CodeIgniter Query Builder does not guarantee consistent type handling for loose values under MySQL STRICT_TRANS_TABLES or STRICT_ALL_TABLES. Automatic escaping is sufficient for SQL injection prevention, it is not sufficient to avoid data-type mismatch errors. Explicit casting or correct PHP types at the application layer are required when strict mode is enabled. PHP escaping overhead is negligible compared to driver prepare/execute and MySQL execution; measure those separately with the framework profiler and driver timing.
CodeIgniter 3.x typically builds SQL strings and sends values as strings to MySQLi. Type fidelity is lost in PHP and MySQL performs implicit conversion. Under strict mode this conversion can raise warnings or errors for mismatched types.
CodeIgniter 4.x uses a more modern database abstraction and can use PDO. With PDO bound parameters, PHP native types can be preserved for the driver, but the Query Builder still does not introspect table schema to cast values to column types. Validation remains delegated to the database engine.
The decision to auto-cast based on column definitions is not implemented and is not documented as guaranteed behavior in either 3.x or 4.x.
Uncertainty note: exact driver behavior varies by CI version, driver choice MySQLi vs PDO, and PDO emulated prepares setting. Current verification is required for your environment.
One missing diagnostic detail that changes the recommendation: which CodeIgniter major version and database driver you are using, and whether PDO emulated prepares are on. That determines whether bound parameter types are preserved to MySQL.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.