Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database getNumRows() #109

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions system/Database/BaseResult.php
Expand Up @@ -137,6 +137,30 @@ public function getResult($type = 'object'): array
return $this->getCustomResultObject($type);
}

//--------------------------------------------------------------------
/**
* Number of rows in the result set
*
* @return int
*/
public function getNumRows()
{
if (is_int($this->numRows))
{
return $this->numRows;
}
elseif (count($this->getResultArray()) > 0)
{
return $this->numRows = count($this->getResultArray());
}
elseif (count($this->getResultObject()) > 0)
{
return $this->numRows = count($this->getResultObject());
}

return $this->numRows = count($this->getResultArray());
}

//--------------------------------------------------------------------

/**
Expand Down
13 changes: 13 additions & 0 deletions system/Database/MySQLi/Result.php
Expand Up @@ -97,6 +97,19 @@ public function getFieldData(): array

return $retval;
}

//--------------------------------------------------------------------
/**
* Number of rows in the result set
*
* @return int
*/
public function getNumRows()
{
return is_int($this->numRows)
? $this->numRows
: $this->numRows = $this->resultID->num_rows;
}

//--------------------------------------------------------------------

Expand Down
22 changes: 20 additions & 2 deletions user_guide_src/source/database/results.rst
Expand Up @@ -264,7 +264,16 @@ Example::
*********************
Result Helper Methods
*********************


**getNumRows()**

The number of rows returned by the query. Make sure to call
the method using your query result object::

$query = $db->query('SELECT * FROM my_table');

echo $query->getNumRows();

**getFieldCount()**

The number of FIELDS (columns) returned by the query. Make sure to call
Expand Down Expand Up @@ -475,7 +484,16 @@ Class Reference

Returns the last row from the result set.

.. php:method:: getFieldCount()
.. php:method:: getNumRows()

:returns: Number of rows in the result set
:rtype: int

Returns the number of rows in the result set.

Usage: see `Result Helper Methods`_.

.. php:method:: getFieldCount()

:returns: Number of fields in the result set
:rtype: int
Expand Down