rfc:mysqli_default_errmode

This is an old revision of the document!


PHP RFC: Change Default mysqli Error Mode

Introduction

The current default error mode for mysqli is silent. This means that when an SQL error occurs, no errors or warnings may be emitted and no exceptions are thrown unless the developer implements their own explicit error handling.

The reasoning behind this was probably to hide very sensitive information present in the error messages on production systems that have display_errors set to true. In hindsight that was not a smart decision. The feature went almost unnoticed and the most common solution to many mysqli-related Stack Overflow questions is to just make people aware of automatic error reporting. By silencing error reporting by default we only made matters worse, as people add or die($mysqli->error) to every mysqli function call unaware of the benefits of PHP error reporting.

Since PHP 8.0 PDO has exception mode enabled by default, it would only make sense to do the same for mysqli. PHP RFC: Change Default PDO Error Mode

Proposal

This RFC proposes to set the default mysqli error reporting mode to exception mode. The new setting would be the same as manually setting the error mode with the following line of code:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

This change will save developers countless hours of debugging. It will also bring the behaviour of this extension in line with PDO and the rest of PHP.

An additional argument for switching the exception mode by default is that since PHP 8.0 a large number of warnings were promoted to Errors. It would make sense to enable automatic error reporting in the form of exception for mysqli errors too.

Backward Incompatible Changes

Existing code that relies on manual error checking will be affected. Codebase that relied on checking the return value of every single mysqli function call will either need to be cleaned up from no longer necessary checks or the silent mode would have to be switched on explicitly if not already done so. To bring back the old behaviour one needs to add only this line before instantiating mysqli class.

mysqli_report(MYSQLI_REPORT_OFF);

Proposed PHP Version(s)

The aim is to include this change in the next PHP 8.x.

RFC Impact

To SAPIs

No changes to SAPIs.

To Existing Extensions

No other extensions should be affected. No new behaviour is being introduced.

To Opcache

No changes.

New Constants

None

php.ini Defaults

None. The error reporting is not available via INI settings.

Open Issues

None.

Unaffected PHP Functionality

No other PHP functionality should be affected. Nikita and I have fixed a number of bugs related to error reporting that didn't throw an exception despite error being present. Switching the default mode should be seamless.

Future Scope

The silent and warning mode could be removed in one of the major versions in the future once the PHP community adjusts. However, that is only wishful thinking.

Proposed Voting Choices

This vote requires a 2/3 majority.

Patches and Tests

Links to any external patches and tests go here.

If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed.

Make it clear if the patch is intended to be the final patch, or is just a prototype.

For changes affecting the core language, you should also provide a patch for the language specification.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

Links to external references, discussions or RFCs

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/mysqli_default_errmode.1611180937.txt.gz · Last modified: 2021/01/20 22:15 by dharman