Exemple #1
0
 /**
  * Gets number of rows returned by a resulset
  *
  *<code>
  *  $result = $connection->query("SELECT * FROM robots ORDER BY name");
  *  echo 'There are ', $result->numRows(), ' rows in the resulset';
  *</code>
  *
  * @return int
  */
 public function numRows()
 {
     $rowCount = $this->_rowCount;
     if ($rowCount === false) {
         switch ($this->_connection->getType()) {
             case 'mysql':
                 $rowCount = $this->_pdoStatement->rowCount();
                 break;
             case 'pgsql':
                 $rowCount = $this->_pdoStatement->rowCount();
                 break;
         }
         if ($rowCount === false) {
             //SQLite/Oracle/SQLServer returns resultsets that to the client eyes (PDO) has an
             //arbitrary number of rows, so we need to perform an extra count to know that
             $sqlStatement = $this->_sqlStatement;
             //If the sql_statement starts with SELECT COUNT(*) we don't make the count
             if (strpos($sqlStatement, 'SELECT COUNT(*) ') !== 0) {
                 $bindParams = $this->_bindParams;
                 $bindTypes = $this->_bindTypes;
                 $matches = null;
                 if (preg_match("/^SELECT\\s+(.*)\$/i", $sqlStatement, $matches) == true) {
                     $rowCount = $this->_connection->query("SELECT COUNT(*) \"numrows\" FROM (SELECT " . $matches[0] . ')', $bindParams, $bindTypes)->fetch()->numRows();
                 }
             } else {
                 $rowCount = 1;
             }
         }
         //Update the value to avoid further calculations
         $this->_rowCount = $rowCount;
     }
     return $rowCount;
 }