Jump to content

PDO MySQL Exceptions


Dead6re

Recommended Posts

Having trouble getting PDO exceptions to work, instead is raising the error handler in PHP. The connection string is fine, the port is supposed to be incorrect so I can generate a log message and show an simple error screen to the client.

 

Warning: PDO::__construct() [pdo.--construct]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://127.0.0.1:3305)

 

PHP Version 5.3.2

MySQL Version  5.1.47-community

 

	try {
		$options = array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);

		$this->db = new PDO($dsn, $username, $password, $options);
		//$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		//$this->db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
	} catch (PDOException $e) {
		$this->throwError('PDODatabase->connect', 'Connection Failed: ' . $e->getMessage());
		return false;
	}

Link to comment
https://forums.phpfreaks.com/topic/213311-pdo-mysql-exceptions/
Share on other sites

In my experience with PDO/PgSql, my code ends up looking like:

 

<?php
class PDO_PgSql extends PDO {
    /**
    * @param string $host
    * @param string $port
    * @param string $user
    * @param string $password
    * @param [string] $dbname
    * @param [string] $port
    * @param [bool] $throw_exceptions
    * @param [bool] $ssl_mode
    * @return PDO_PgSql
    */
    public function __construct( $host, $user, $password, $dbname = null, $port = null, $throw_exceptions = true, $ssl_mode = false ) {
        //
        // Build the connection string
        if( $port === null ) {
            $port = '5432'; // Default pgsql port
        }
        $dsn = sprintf(
            'pgsql:host=%s port=%s user=%s password=%s',
            $host,
            $port,
            $user,
            $password
        );
        if( $dbname !== null ) {
            $dsn .= ' dbname=' . $dbname;
        }
        if( $ssl_mode === true ) {
            $dsn .= ' sslmode=require';
        }else{
            $dsn .= ' sslmode=disable';
        }
        //
        // Driver options
        $driver_opts = array();
        if( $throw_exceptions === true ) {
            $driver_opts[ PDO::ATTR_ERRMODE ] = PDO::ERRMODE_EXCEPTION;
        }else{
            $driver_opts[ PDO::ATTR_ERRMODE ] = PDO::ERRMODE_WARNING;
        }
        //
        // Create
        parent::__construct( $dsn, null, null, $driver_opts );
        //
        // Custom PDOStatement class
        $this->setAttribute( PDO::ATTR_STATEMENT_CLASS, array( 'PDOStatement_PgSql' ) );
        //
        // Throw exceptions.  Yes, set it in two places
        if( $throw_exceptions === true ) {
            $this->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        }
    }
    
...
}
?>

 

You'll notice that I turn on exception handling via options in the constructor as well as explicitly calling the setAttribute() method.  It doesn't seem to hurt anything and its the only way I was able to get it to work uniformly on Windows and Linux with the versions of PHP I run.

 

Hope that helps.

  Quote

Seems that PDO is supposed to act the way it is as the warning I have is a E_NOTICE error. Generally a PHP config is set with E_ALL & ~E_NOTICE for production environments and thus isn't seen. Changing the timeout seem to help reponsiveness in the script if the host could not be found.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.