*/
function numFields( $res ) {
$r = $res instanceof ResultWrapper ? $res->result : $res;
-
- return is_array( $r ) ? count( $r[0] ) : 0;
+ if ( is_array($r) && count( $r ) > 0 ){
+ // The size of the result array is twice the number of fields. (Bug: 65578)
+ return count( $r[0] ) / 2 ;
+ } else {
+ // If the result is empty return 0
+ return 0;
+ }
}
/**
$row = $res->fetchRow();
$this->assertFalse( (bool)$row['a'] );
}
+
+ /**
+ * @covers DatabaseSqlite::numFields
+ */
+ public function testNumFields() {
+ $db = new DatabaseSqliteStandalone( ':memory:' );
+
+ $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
+ $this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Failed to create table a" );
+ $res = $db->select( 'a' , '*');
+ $this->assertEquals( 0, $db->numFields($res), "expects to get 0 fields for an empty table" );
+ $insertion = $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ );
+ $this->assertTrue( $insertion, "Insertion failed" );
+ $res = $db->select( 'a' , '*');
+ $this->assertEquals( 1, $db->numFields($res), "wrong number of fields" );
+
+ $this->assertTrue( $db->close(), "closing database" );
+ }
}