Topic on Extension talk:OdbcDatabase

PHP Stack trace result upon empty result set

3
70.62.245.194 (talkcontribs)

This extension works great for retrieving data from our SQL2012 databases. However, when I use a legit query that happens to have an empty result set, the interface between ExternalData and this ODBC Extension fails and a php stack trace is all I see. Am I supposed to first retrieve a count of rows before proceeding to actually retrieve records? That doesn't sound right. Please advise.

71.64.103.160 (talkcontribs)

I'll answer my own question I guess, the patch listed below avoids complaining about there being no row to retrieve when 0 zero rows were returned by the query:

--- OdbcDatabase.body.php.original	2014-12-01 18:14:08.000000000 -0500
+++ OdbcDatabase.body.php	2015-06-04 23:04:34.127805703 -0400
@@ -128,16 +128,18 @@
 		}
 
 		$array = null;
-		$row = odbc_fetch_row( $res );
-		if ( $row ) {
-			$this->mRowNum++;
-			$nCols = odbc_num_fields( $res );
-			for ( $i = 0; $i < $nCols; $i++ ) {
-				$array[$i] = odbc_result( $res, $i+1 );
-			}
-		} else if ( $this->mRowNum <= $this->mAffectedRows ) {
-			if ( $this->lastErrno() ) {
-				throw new DBUnexpectedError( $this, wfMessage( 'odbcdatabase-fetch-row-error', $this->lastErrno(), htmlspecialchars( $this->lastErro
+		if ( $this->mAffectedRows > 0 ) {
+			$row = odbc_fetch_row( $res );
+			if ( $row ) {
+				$this->mRowNum++;
+				$nCols = odbc_num_fields( $res );
+				for ( $i = 0; $i < $nCols; $i++ ) {
+					$array[$i] = odbc_result( $res, $i+1 );
+				}
+			} else if ( $this->mRowNum <= $this->mAffectedRows ) {
+				if ( $this->lastErrno() ) {
+					throw new DBUnexpectedError( $this, wfMessage( 'odbcdatabase-fetch-row-error', $this->lastErrno(), htmlspecialchars( $this->
+				}
 			}
 		}
 		return $array;
71.64.103.160 (talkcontribs)

Actually, this patch is better for two reasons:

  • the exception logic only triggers on failures
  • it handles the single row case!
--- OdbcDatabase.body.php.original	2014-12-01 18:14:08.000000000 -0500
+++ OdbcDatabase.body.php	2015-06-04 23:37:04.084804367 -0400
@@ -136,7 +136,7 @@
 				$array[$i] = odbc_result( $res, $i+1 );
 			}
 		} else if ( $this->mRowNum <= $this->mAffectedRows ) {
-			if ( $this->lastErrno() ) {
+			if ( ($this->mAffectedRows > 0) && $this->lastErrno() ) {
 				throw new DBUnexpectedError( $this, wfMessage( 'odbcdatabase-fetch-row-error', $this->lastErrno(), htmlspecialchars( $this->lastError() ) ) );
 			}
 		}
Reply to "PHP Stack trace result upon empty result set"