src/Util/PDOX.php
function queryReturnError($sql, $arr=FALSE, $error_log=TRUE) {
        $errormode = $this->getAttribute(\PDO::ATTR_ERRMODE);
        if ( $errormode != \PDO::ERRMODE_EXCEPTION) {
            $this->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        }
        $q = FALSE;
        $success = FALSE;
        $message = '';
        if ( $arr !== FALSE && ! is_array($arr) ) $arr = Array($arr);
        $start = microtime(true);
        // debug_log($sql, $arr);
        try {
            $q = $this->prepare($sql);
            if ( $arr === FALSE ) {
                $success = $q->execute();
            } else {
                $success = $q->execute($arr);
            }
        } catch(\Exception $e) {
            $success = FALSE;
            $message = $e->getMessage();
            if ( $error_log ) error_log($message);
        }
        if ( ! is_object($q) ) $q = new \stdClass();
        if ( isset( $q->success ) ) {
            error_log("\PDO::Statement should not have success member");
            die("\PDO::Statement should not have success member"); // with error_log
        }
        $q->success = $success;
        if ( isset( $q->ellapsed_time ) ) {
            error_log("\PDO::Statement should not have ellapsed_time member");
            die("\PDO::Statement should not have ellapsed_time member"); // with error_log
        }
        $q->ellapsed_time = microtime(true)-$start;
        if ( $this->slow_query < 0 || ($this->slow_query > 0 && $q->ellapsed_time > $this->slow_query ) ) {
            $dbt = U::getCaller(2);
            $caller_uri = U::get($_SERVER,'REQUEST_URI');
            error_log("PDOX Slow Query:".$q->ellapsed_time.' '.$caller_uri.' '.$dbt);
        }

        // In case we build this...
        if ( !isset($q->errorCode) ) $q->errorCode = '42000';
        if ( !isset($q->errorInfo) ) $q->errorInfo = Array('42000', '42000', $message);
        if ( !isset($q->errorImplode) ) $q->errorImplode = implode(':',$q->errorInfo);
        // Restore ERRMODE if we changed it
        if ( $errormode != \PDO::ERRMODE_EXCEPTION) {
            $this->setAttribute(\PDO::ATTR_ERRMODE, $errormode);
        }
        return $q;
    }
function queryReturnError($sql, $arr=FALSE, $error_log=TRUE) {
        $errormode = $this->getAttribute(\PDO::ATTR_ERRMODE);
        if ( $errormode != \PDO::ERRMODE_EXCEPTION) {
            $this->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        }
        $q = FALSE;
        $success = FALSE;
        $message = '';
        if ( $arr !== FALSE && ! is_array($arr) ) $arr = Array($arr);
        $start = microtime(true);
        // debug_log($sql, $arr);
        try {
            $q = $this->prepare($sql);
            if ( $arr === FALSE ) {
                $success = $q->execute();
            } else {
                $success = $q->execute($arr);
            }
        } catch(\Exception $e) {
            $success = FALSE;
            $message = $e->getMessage();
            if ( $error_log ) error_log($message);
        }
        if ( ! is_object($q) ) $q = new \stdClass();
        if ( isset( $q->success ) ) {
            error_log("\PDO::Statement should not have success member");
            die("\PDO::Statement should not have success member"); // with error_log
        }
        $q->success = $success;
        if ( isset( $q->ellapsed_time ) ) {
            error_log("\PDO::Statement should not have ellapsed_time member");
            die("\PDO::Statement should not have ellapsed_time member"); // with error_log
        }
        $q->ellapsed_time = microtime(true)-$start;
        if ( $this->slow_query < 0 || ($this->slow_query > 0 && $q->ellapsed_time > $this->slow_query ) ) {
            $dbt = U::getCaller(2);
            $caller_uri = U::get($_SERVER,'REQUEST_URI');
            error_log("PDOX Slow Query:".$q->ellapsed_time.' '.$caller_uri.' '.$dbt);
        }

        // In case we build this...
        if ( !isset($q->errorCode) ) $q->errorCode = '42000';
        if ( !isset($q->errorInfo) ) $q->errorInfo = Array('42000', '42000', $message);
        if ( !isset($q->errorImplode) ) $q->errorImplode = implode(':',$q->errorInfo);
        // Restore ERRMODE if we changed it
        if ( $errormode != \PDO::ERRMODE_EXCEPTION) {
            $this->setAttribute(\PDO::ATTR_ERRMODE, $errormode);
        }
        return $q;
    }
function queryReturnError ( $ sql , $ arr = FALSE , $ error_log = TRUE ) { $ errormode = $ this -> getAttribute ( \ PDO :: ATTR_ERRMODE ) ; if ( $ errormode != \ PDO :: ERRMODE_EXCEPTION ) { $ this -> setAttribute ( \ PDO :: ATTR_ERRMODE , \ PDO :: ERRMODE_EXCEPTION ) ; } $ q = FALSE ; $ success = FALSE ; $ message = '' ; if ( $ arr !== FALSE && ! is_array ( $ arr ) ) $ arr = Array ( $ arr ) ; $ start = microtime ( true ) ; // debug_log($sql, $arr); try { $ q = $ this -> prepare ( $ sql ) ; if ( $ arr === FALSE ) { $ success = $ q -> execute ( ) ; } else { $ success = $ q -> execute ( $ arr ) ; } } catch ( \ Exception $ e ) { $ success = FALSE ; $ message = $ e -> getMessage ( ) ; if ( $ error_log ) error_log ( $ message ) ; } if ( ! is_object ( $ q ) ) $ q = new \ stdClass ( ) ; if ( isset ( $ q -> success ) ) { error_log ( "\PDO::Statement should not have success member" ) ; die ( "\PDO::Statement should not have success member" ) ; // with error_log } $ q -> success = $ success ; if ( isset ( $ q -> ellapsed_time ) ) { error_log ( "\PDO::Statement should not have ellapsed_time member" ) ; die ( "\PDO::Statement should not have ellapsed_time member" ) ; // with error_log } $ q -> ellapsed_time = microtime ( true ) - $ start ; if ( $ this -> slow_query < 0 || ( $ this -> slow_query > 0 && $ q -> ellapsed_time > $ this -> slow_query ) ) { $ dbt = U :: getCaller ( 2 ) ; $ caller_uri = U :: get ( $ _SERVER , 'REQUEST_URI' ) ; error_log ( "PDOX Slow Query:" . $ q -> ellapsed_time . ' ' . $ caller_uri . ' ' . $ dbt ) ; } // In case we build this... if ( ! isset ( $ q -> errorCode ) ) $ q -> errorCode = '42000' ; if ( ! isset ( $ q -> errorInfo ) ) $ q -> errorInfo = Array ( '42000' , '42000' , $ message ) ; if ( ! isset ( $ q -> errorImplode ) ) $ q -> errorImplode = implode ( ':' , $ q -> errorInfo ) ; // Restore ERRMODE if we changed it if ( $ errormode != \ PDO :: ERRMODE_EXCEPTION ) { $ this -> setAttribute ( \ PDO :: ATTR_ERRMODE , $ errormode ) ; } return $ q ; }
Prepare and execute an SQL query with lots of error checking.

This routine will call prepare() and then execute() with the
resulting PDOStatement and return the PDOStatement.
If the prepare() fails, we fake up a stdClass() with a few
fields that mimic a simple failed execute().

$stmt->errorCode
$stmt->errorInfo

We also augment the real or fake PDOStatement with these fields:

$stmt->success
$stmt->ellapsed_time
$stmt->errorImplode

<var>$stmt->success</var> is TRUE/FALSE based on the success of the operation
to simplify error checking

<var>$stmt->ellapsed_time</var> includes the length of time the query took

<var>$stmt->errorImplode</var> an imploded version of errorInfo suitable for
dropping into a log.

@param $sql The SQL to execute in a string.
@param $arr An optional array of the substitition values if needed by the query
@param $error_log Indicates whether or not errors are to be logged. Default is TRUE.
@return \PDOStatement  This is either the real PDOStatement from the prepare() call
or a stdClass mocked to have error indications as described above.
Prepare and execute an SQL query with lots of error checking .