Skip to content

Commit 2773849

Browse files
committed
Fix a issue with CI_DB_pdo_result::num_rows()
In case of SELECT queries PDOStatement::rowCount doesn't work as expected. This commit makes it returns the expected value.
1 parent 9fa8d40 commit 2773849

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

system/database/drivers/pdo/pdo_result.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,18 @@ class CI_DB_pdo_result extends CI_DB_result {
3434
*/
3535
function num_rows()
3636
{
37-
return $this->result_id->rowCount();
37+
if (is_numeric(stripos($this->result_id->queryString, 'SELECT')))
38+
{
39+
$dbh = $this->conn_id;
40+
$query = $dbh->query($this->result_id->queryString);
41+
$result = $query->fetchAll();
42+
unset($dbh, $query);
43+
return count($result);
44+
}
45+
else
46+
{
47+
return $this->result_id->rowCount();
48+
}
3849
}
3950

4051
// --------------------------------------------------------------------

user_guide/changelog.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ <h3>Bug fixes for 2.1.1</h3>
8888
<li>Fixed a bug - CSRF cookie value was allowed to be any (non-empty) string before being written to the output, making code injection a risk.</li>
8989
<li>Fixed a bug (#726) - PDO put a 'dbname' argument in it's connection string regardless of the database platform in use, which made it impossible to use SQLite.</li>
9090
<li>Fixed a bug - CI_DB_pdo_driver::affect_row was not being initialized properly with SELECT queries, cause it was relying on PDOStatement::rowCount().</li>
91+
<li>Fixed a bug - CI_DB_pdo_result::num_rows() was not returning properly value with SELECT queries, cause it was relying on PDOStatement::rowCount().</li>
9192
</ul>
9293

9394

0 commit comments

Comments
 (0)