php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63812 PDO Triggers Warning(s) Regardless of Error Handling Strategy
Submitted: 2012-12-20 05:25 UTC Modified: 2020-03-22 04:22 UTC
Votes:45
Avg. Score:4.5 ± 0.7
Reproduced:45 of 45 (100.0%)
Same Version:1 (2.2%)
Same OS:32 (71.1%)
From: sergey at shymko dot net Assigned: cmb (profile)
Status: No Feedback Package: PDO MySQL
PHP Version: 5.3.19 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2012-12-20 05:25 UTC] sergey at shymko dot net
Description:
------------
PDO triggers warning(s) regardless of the chosen error handling strategy 
http://php.net/manual/en/pdo.error-handling.php

Environment:
- mysql  Ver 14.14 Distrib 5.5.22, for Linux (x86_64) using  EditLine wrapper

Test script:
---------------
<?php

$adapter = new PDO('mysql:host=localhost;db_name=test', 'root', '');

$adapter->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
assert($adapter->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_EXCEPTION);

$waitTimeout = 1;

$adapter->exec("SET @@session.wait_timeout = {$waitTimeout}");
$statement = $adapter->query('SELECT @@session.wait_timeout');
assert($statement->fetchColumn() == $waitTimeout);

/**
 * Ensure 'MySQL server has gone away' conditions are met
 */
sleep($waitTimeout + 1);

$adapter->query('SELECT 1');


Expected result:
----------------
- PDOException with message 'SQLSTATE[HY000]: General error: 2006 MySQL server 
has gone away'
- No warnings (because of the chosen PDO error handling strategy)

Actual result:
--------------
1. When connection type is TCP/IP:
   1. Warning: PDO::query() [pdo.query]: MySQL server has gone away
   2. Warning: PDO::query() [pdo.query]: Error reading result set's header
1'. When connection type is Unix socket:
   1. Warning: Error while sending QUERY packet. PID=18586
2. PDOException with message 'SQLSTATE[HY000]: General error: 2006 MySQL server 
has gone away'

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-20 05:34 UTC] sergey at shymko dot net
Relates to https://bugs.php.net/bug.php?id=63546
 [2013-05-03 05:09 UTC] fog at yandex dot ru
There is another similar problem, sometimes connection fails with message:

Warning: PDO::__construct(): MySQL server has gone away in [...file_path...] on line N

It happens despiting that PDO::ATTR_ERRMODE is set to PDO::ERRMODE_EXCEPTION
 [2014-01-01 12:34 UTC] felipe@php.net
-Package: PDO related +Package: PDO MySQL
 [2015-01-20 03:10 UTC] nospampls at nospam dot org
I have the same problem, regardless PDO::ERRMODE_EXCEPTION I sometimes get warnings or both warnings and the pdoexception equivalent at the same time (!!!).

please fix
 [2015-02-11 13:20 UTC] oxygenus at gmail dot com
PHP 5.6.5 (cli) (built: Jan 21 2015 17:50:29)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.7, Copyright (c) 2002-2015, by Derick Rethans


set_error_handler is ignored and has no effect.

Also, as others have commented, the PDO exception mode is also ignored.

The warning is sent to php://stdout no matter what, even if making sure the PDO instance is destroyed and disconnected.

Apparently, the warning is sent to php://stdout during garbage collection.

It makes JSONRPC servers and other stuff impossible to use.
 [2015-06-23 23:16 UTC] klenium2 at gmail dot com
Similar problem:

try { new PDO(
	"mysql:dbname=test;host=notalocalhost",
	$user, $pass,
	[PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT]
); }
catch (PDOException $e) { die("msg: ".$e->getMessage()); }

Result:

Warning: PDO::__construct(): in C:\file.php on line 2
msg: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Nincs ilyen ismert állomás.
 [2016-02-23 09:39 UTC] oxygenus at gmail dot com
Still present in PHP 7.
 [2016-06-15 07:57 UTC] gaobin3243 at 126 dot com
I have the same problem, regardless PDO::ERRMODE_EXCEPTION I sometimes get warnings or both warnings and the pdoexception equivalent at the same time (!!!).
 [2017-04-20 15:38 UTC] justpusher at gmail dot com
We faced this bug too after upgrading our servers to PHP 5.6.30.
Ubuntu 16.04.2 LTS 

The test script given in the bug description reproduces the bug.
 [2018-11-09 13:17 UTC] jferrer at data-solutions dot com
This bug is a ******* joke
I don't understand how something like this is still not fixed in php 7.0.3

This is how i "fixed" it:

// Convert NOTICE, WARNING, ... in Exceptions
$convert_error_to_exception = function ($level, $message, $file, $line, $context=null){
    throw new ErrorException($message, 0, $level, $file, $line);
};

set_error_handler($convert_error_to_exception);

I convert Warnings in ErrorException which makes them catchable
 [2019-02-27 07:29 UTC] moritz dot friedrich at messengerpeople dot com
This bug is, as of today, more than six years old. Is there anyone on the developer team that might want to comment on it, provide some information on how to mitigate the issue or at least add a wontfix flag so we know we'll have to deal with this ourselves?
 [2020-03-09 17:41 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2020-03-09 17:41 UTC] cmb@php.net
This issue is supposed to be [1] as of PHP 7.4.0.  I don't think
that back-porting the fix to PHP 7.3 makes much sense at this
point.

[1] <http://git.php.net/?p=php-src.git;a=commit;h=2856afc70e50b85424b2bd2d6653020679160a0b>
 [2020-03-22 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2023-04-04 23:39 UTC] info at daniel-marschall dot de
The bug is now 11 years old and nobody cares :-(

In the latest version of PHP 8.2, PDO shows a PHP warning, even if PDO::ERRMODE_SILENT is applied. This is wrong. Silent means silent. Please fix this.
 [2023-04-04 23:59 UTC] info at daniel-marschall dot de
Addendum:

My setup:
- PHP 8.2.0
- PDO, driver mysql
- mysql  Ver 15.1 Distrib 10.4.22-MariaDB
- Windows 10 host
- PDO:ATTR_ERRMODE is set to PDO:ERRMODE_SILENT

I made two tests. One works as expected, the other not.

First test:

- Query "SELECT * from NONEXISTING" via Prepared Statement
- No parameters were given in the prepared statement
- GOOD: $ps->errorInfo() is ['42S02', '1146', "Table 'dbname.NONEXISTING' doesn't exist"].
- GOOD: No PHP warning is shown

Second test:

- Query "SELECT * from NONEXISTING" via Prepared Statement
- 1 parameter was given (yes, this is clearly wrong)
- BAD: $ps->errorInfo() is ['HY093', null, null]  (there is no driver specific error message!)
- BAD: PHP Warning is shown "Invalid parameter number: number of bound variables does not match number of tokens"
- Possible error: This error message is probably accidentally output to PHP warning instead of being put into errorInfo().
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 11:01:28 2024 UTC