function Result::rowCount

Returns the number of rows matched by the last SQL statement.

Return value

int The number of rows matched by the last DELETE, INSERT, or UPDATE statement executed or throws \Drupal\Core\Database\RowCountException if the last executed statement was SELECT.

Overrides ResultBase::rowCount

File

core/modules/mysqli/src/Driver/Database/mysqli/Result.php, line 43

Class

Result
Class for mysqli-provided results of a data query language (DQL) statement.

Namespace

Drupal\mysqli\Driver\Database\mysqli

Code

public function rowCount() : ?int {
  // The most accurate value to return for Drupal here is the first
  // occurrence of an integer in the string stored by the connection's
  // $info property.
  // This is something like 'Rows matched: 1  Changed: 1  Warnings: 0' for
  // UPDATE or DELETE operations, 'Records: 2  Duplicates: 1  Warnings: 0'
  // for INSERT ones.
  // This however requires a regex parsing of the string which is expensive;
  // $affected_rows would be less accurate but much faster. We would need
  // Drupal to be less strict in testing, and never rely on this value in
  // runtime (which would be healthy anyway).
  if ($this->mysqliConnection->info !== NULL) {
    $matches = [];
    if (preg_match('/\\s(\\d+)\\s/', $this->mysqliConnection->info, $matches) === 1) {
      return (int) $matches[0];
    }
    else {
      throw new DatabaseExceptionWrapper('Invalid data in the $info property of the mysqli connection - ' . $this->mysqliConnection->info);
    }
  }
  elseif ($this->mysqliConnection->affected_rows !== NULL) {
    return $this->mysqliConnection->affected_rows;
  }
  throw new DatabaseExceptionWrapper('Unable to retrieve affected rows data');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.