Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Work-around for PHP5's PDO rowCount MySQL issue

Tags:

php

pdo

I've recently started work on a new project using PHP5 and want to use their PDO classes for it. The problem is that the MySQL PDO Driver doesn't support rowCount() so there's no way to run a query and then get the number of affected rows, or rows returned, which is a pretty big issue as far as I'm concerned. I was wondering if anyone else has dealt with this before and what you've done to work around it. Having to do a fetch() or fetchAll() to check if any rows were affected or returned seems like a hack to me, I'd rather just do $stmt->numRows() or something similar.

like image 728
Steven Surowiec Avatar asked Jan 20 '09 03:01

Steven Surowiec


1 Answers

You can issue a SELECT FOUND_ROWS() query right after the original SELECT query to get row count.

$pdo->query("SELECT * FROM users");
$foundRows = $pdo->query("SELECT FOUND_ROWS()")->fetchColumn();

See also: MySQL Docs on FOUND_ROWS()

like image 156
Imran Avatar answered Oct 21 '22 01:10

Imran