NYCPHP Meetup

NYPHP.org

[nycphp-talk] Global error handling ...

Analysis & Solutions danielc at analysisandsolutions.com
Wed Sep 24 12:42:38 EDT 2003


Hi Jon:

On Wed, Sep 24, 2003 at 09:26:30AM -0400, jon baer wrote:
> 
> but i noticed that (could be my config) that mysql_select_db will not pass
> error to any handlers (using set_error_handler).

When some mysql* functions fail, they cause PHP to die.  Finding out
what's going on is doable via $php_errormsg, which requires track_errors
to be on (in php.ini for example).

Here are some snippets from my SQL Solution class:

    //  Connect to server
    $this->SQLConnection = @mysql_connect($this->SQLHost, $this->SQLUser,
            $this->SQLPassword) or die
            ( $this->KillQuery($FileName, $FileLine, $php_errormsg) );

    //  Select a db
    $this->SQLDbHandle = @mysql_select_db($this->SQLDbName, 
            $this->SQLConnection)
            or die ( $this->KillQuery($FileName, $FileLine,
            'Could not select database. '
            . 'Invalid database name or connection ID.') );


    //  Run a query
    $php_errormsg = '';
    if ( $this->SQLRecordSet = @mysql_query($this->SQLQueryString,
            $this->SQLConnection) ) {

        if ( ! $this->SQLRecordSetFieldCount =
                @mysql_num_fields($this->SQLRecordSet) ) {
            $this->SQLRecordSetFieldCount = 0;
        }

        if ( ! $this->SQLRecordSetRowCount =
                @mysql_num_rows($this->SQLRecordSet) ) {
            $this->SQLRecordSetRowCount = 0;
        }

    } elseif ( $php_errormsg == '' ) {
        # Probably a database error.
        $this->KillQuery( $FileName, $FileLine,
                @mysql_error($this->SQLConnection) );

    } else {
        # Some PHP error.  Probably a bad Connection.  Complain.
        $this->KillQuery($FileName, $FileLine, $php_errormsg);

    }

Enjoy,

--Dan

-- 
     FREE scripts that make web and database programming easier
           http://www.analysisandsolutions.com/software/
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7th Ave #4AJ, Brooklyn NY    v: 718-854-0335   f: 718-854-0409



More information about the talk mailing list