php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77716 PDO show warning event the PDO::ERRMODE_EXCEPTION has been set
Submitted: 2019-03-09 17:32 UTC Modified: 2020-12-11 14:38 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:0 (0.0%)
From: shyandsy at gmail dot com Assigned: nikic (profile)
Status: Closed Package: PDO Core
PHP Version: 7.2.16 OS: CentOS 7.3.1611
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
37 + 2 = ?
Subscribe to this entry?

 
 [2019-03-09 17:32 UTC] shyandsy at gmail dot com
Description:
------------
The PDO shows warning event I have set the error mode to exception

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

define(DB_HOSTNAME, "");
define(DB_PORT, "");
define(DB_DATABASE, "");
define(DB_USERNAME, "");
define(DB_PASSWORD, "");

echo DB_HOSTNAME . "\n";

try{
    $dsn = "mysql:host=" . DB_HOSTNAME. ";port=" . DB_PORT . ";dbname=" . DB_DATABASE;
    $pdo = new PDO($dsn,DB_USERNAME,DB_PASSWORD, [
        PDO::ATTR_PERSISTENT => true,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
    $pdo->query("SET wait_timeout=1;");
    sleep(3);
    $pdo->query("select * from dev_user");

}catch(\PDOException $e){
    echo $e->getMessage() . "<br>";
}


Expected result:
----------------
SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

Actual result:
--------------
[root@localhost experiment]# php MysqlGoneAway.php
192.168.216.133
PHP Warning:  PDO::query(): MySQL server has gone away in /home/wwwroot/www.xxx.com/experiment/MysqlGoneAway.php on line 21
PHP Warning:  PDO::query(): Error reading result set's header in /home/wwwroot/www.xxx.com/experiment/MysqlGoneAway.php on line 21
SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-09 18:02 UTC] danack@php.net
-Status: Open +Status: Feedback
 [2019-03-09 18:02 UTC] danack@php.net
I'm unable to reproduce this error. Please can you check that you are posting the exact script that shows the error.

Running the code as posted, I get warnings about:

PHP Warning:  Use of undefined constant DB_HOSTNAME - assumed 'DB_HOSTNAME' (this will throw an Error in a future version of PHP) in /var/app/test.php on line 15

Fixing those errors I get a single error, that was caught in the exception block:

SQLSTATE[HY000] [2002] No such file or directory


i.e. the output looks different from what you posted and looks like the correct behaviour.


For example, in your output you have "192.168.216.133" - but you have "define(DB_HOSTNAME, "");" in the code example.
 [2019-03-09 19:26 UTC] shyandsy at gmail dot com
-Status: Feedback +Status: Open
 [2019-03-09 19:26 UTC] shyandsy at gmail dot com
u have to set these constant begin with DB_ before u g
 the code
 [2019-03-11 22:43 UTC] kalle@php.net
-Status: Open +Status: Feedback
 [2019-03-11 22:43 UTC] kalle@php.net
I believe this warning comes from mysqlnd, afair mysqlnd only issues warnings and does not know about PDO's errormode being set to Exception, hence why it does not honor any ERRMODE_EXCEPTION value.

Can you confirm you are using mysqlnd?
 [2019-03-11 22:52 UTC] shyandsy at gmail dot com
-Status: Feedback +Status: Open
 [2019-03-11 22:52 UTC] shyandsy at gmail dot com
1. I checked the output of php -m, yeah I m using mysqlnd.
2. This error can be catched by error handler, like following:
```
set_error_handler(function($code, $message, $file, $line) use($log, $config) {
	
    // 单独处理mysql gone away
    if(strpos($message, 'server has gone away') !== false){
        return false;
    }
    
    ........
```
3. I checked the source code of mysqlnd, looks you are right


it's just inconsistant to issue a warning if I set the error mode to exception
 [2019-09-10 19:45 UTC] alessandro dot lai85 at gmail dot com
This seems a dupe of https://bugs.php.net/bug.php?id=63812 (which is WAAAY older, since 5.3!!)
 [2020-12-11 14:38 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-12-11 14:38 UTC] nikic@php.net
This issue has been fixed in the meantime.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Oct 07 17:01:28 2024 UTC