php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53185 PDO_MYSQL constructor, PHP warning w/ Exception error mode
Submitted: 2010-10-27 17:17 UTC Modified: 2016-07-31 04:25 UTC
Votes:18
Avg. Score:4.4 ± 0.8
Reproduced:17 of 17 (100.0%)
Same Version:4 (23.5%)
Same OS:9 (52.9%)
From: alexc223 at googlemail dot com Assigned:
Status: Open Package: PDO MySQL
PHP Version: 5.3.3 OS: Linux
Private report: No CVE-ID:
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: alexc223 at googlemail dot com
New email:
PHP Version: OS:

 

 [2010-10-27 17:17 UTC] alexc223 at googlemail dot com
Description:
------------
A PHP warning is generated even though I've explicitly set the PDO error mode to 
use exceptions. This warning occurs when the MySQL socket does not exist (for 
example, if MySQL is not yet started).

This makes it quite impossible to handle.

Test script:
---------------
<?php
   try {
      $pdo = new Pdo( 'mysql:host=localhost;dbname=test', 'user', 'pass', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) );
   } catch ( PDOException $e ) {
      echo 'unable to connect';
   }
?>


Expected result:
----------------
unable to connect

Actual result:
--------------
Warning: PDO::__construct(): [2002] No such file or directory (trying to connect 
via unix:///var/run/mysqld/mysqld.sock) in php shell code on line 2        
unable to connect

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-02 22:12 UTC] felipe@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: mysql
 [2010-11-02 22:12 UTC] felipe@php.net
The warning come from mysqlnd_connect() call that nothing knows about such PDO feature...
 [2010-12-22 17:19 UTC] alexc223 at googlemail dot com
Maybe there needs to be a way for it to know? Either way, it's highly annoying 
since there is very little ways to handle it.
 [2011-01-09 06:11 UTC] kalle@php.net
-Assigned To: mysql +Assigned To: kalle
 [2012-05-15 11:37 UTC] jach at wayf dot dk
Same error on OSX version 3.5.10
 [2012-06-24 10:15 UTC] pineain at gmail dot com
Same problem on Windows 7 x64
 [2014-01-01 12:47 UTC] felipe@php.net
-Package: PDO related +Package: PDO MySQL
 [2014-12-15 10:28 UTC] s dot adrichem at daisycon dot com
In php 5.3.23 this did not happen upon disconnects and such, eventough we never set the mode, now though in PHP 5.6.3 it does happen even if we change the modes, with the following simple code.

	$sHostName = "";
	$sUserName = "";
	$sPassword = "";
	$sDefaultDb = "";
	$sTestQuery = " SELECT NOW()";

	$iPort = 3306;
	

	$oDb = new PDO(
		'mysql:host='.$sHostName.';port=' . $iPort,
		$sUserName,
		$sPassword
	);
	$oDb->setAttribute(
		PDO::ATTR_ERRMODE, 
		PDO::ERRMODE_EXCEPTION
	);
	var_dump($oDb->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_EXCEPTION);
	$sQuery = "SET WAIT_TIMEOUT = 1";
	$oResult = $oDb->prepare( $sQuery );
	$oResult->execute();
	$sQuery = "use " . $sDefaultDb;
	$oResult = $oDb->prepare( $sQuery );
	$oResult->execute();

	sleep(3);

	$oResult = $oDb->prepare( $sTestQuery );
	$oResult->execute();

	while ($aRow = $oResult->fetch( PDO::FETCH_ASSOC ))
	{
		var_dump($aRow);
	}

I get the following warnings: 
Warning: PDOStatement::execute(): MySQL server has gone away in pdotest.php on line 31
Warning: PDOStatement::execute(): Error reading result set's header in pdotest.php on line 31

And then the expected exception
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2006 MySQL server has gone away' in pdotest.php on line 31


Same goes for silent mode, so whats the point of these modes then ;)
 [2015-02-03 17:26 UTC] tom dot castleman at b3-it dot com
This error is present in 5.6.4.

PDO::__construct emits an E_WARNING despite passing PDO::ATTR_ERRMODE option to it:

$pdo = new PDO('mysql:host=foo;dbname=bar', 'user', 'pass', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

Expected Result:
----------------
PDOException - MySQL server has gone away

Actual Result:
--------------
E_WARNING: PDO::__construct(): MySQL server has gone away
 [2016-07-31 04:25 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: kalle +Assigned To:
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Mon Apr 24 15:01:45 2017 UTC