There are many conflicting statements around. What is the best way to get the row count using PDO in PHP? Before using PDO, I just simply used mysql_num_rows
.
fetchColumn()
used if want to get count of record [effisien]
$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
$res = $conn->query($sql);
$count = $res->fetchColumn(); // ex = 2
query()
used if want to retrieve data and count of record [options]
$sql = "SELECT * FROM fruit WHERE calories > 100";
$res = $conn->query($sql);
if ( $res->rowCount() > 0) {
foreach ( $res as $row ) {
print "Name: {$row['NAME']} <br />";
}
}
else {
print "No rows matched the query.";
}
PDOStatement::rowCount
As I wrote previously in an answer to a similar question, the only reason mysql_num_rows()
worked is because it was internally fetching all the rows to give you that information, even if it didn't seem like it to you.
So in PDO, your options are:
count()
on it.SELECT COUNT(*)
, as karim79 suggested.SQL_CALC_FOUND_ROWS
or a LIMIT
clause (in which case the number of rows that were returned by the query and the number returned by FOUND_ROWS()
may differ). However, this function is deprecated and will be removed in the future.This is super late, but I ran into the problem and I do this:
function countAll($table){
$dbh = dbConnect();
$sql = "select * from `$table`";
$stmt = $dbh->prepare($sql);
try { $stmt->execute();}
catch(PDOException $e){echo $e->getMessage();}
return $stmt->rowCount();
It's really simple, and easy. :)
I tried $count = $stmt->rowCount();
with Oracle 11.2 and it did not work.
I decided to used a for loop as show below.
$count = "";
$stmt = $conn->prepare($sql);
$stmt->execute();
echo "<table border='1'>\n";
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
$count++;
echo "<tr>\n";
foreach ($row as $item) {
echo "<td class='td2'>".($item !== null ? htmlentities($item, ENT_QUOTES):" ")."</td>\n";
} //foreach ends
}// while ends
echo "</table>\n";
//echo " no of rows : ". oci_num_rows($stmt);
//equivalent in pdo::prepare statement
echo "no.of rows :".$count;
I ended up using this:
$result = $db->query($query)->fetchAll();
if (count($result) > 0) {
foreach ($result as $row) {
echo $row['blah'] . '<br />';
}
} else {
echo "<p>Nothing matched your query.</p>";
}
when you make a COUNT(*) in your mysql statement like in
$q = $db->query("SELECT COUNT(*) FROM ...");
your mysql query is already counting the number of result why counting again in php? to get the result of your mysql
$q = $db->query("SELECT COUNT(*) as counted FROM ...");
$nb = $q->fetch(PDO::FETCH_OBJ);
$nb = $nb->counted;
and $nb
will contain the integer you have counted with your mysql statement
a bit long to write but fast to execute
Edit: sorry for the wrong post but as some example show query with count in, I was suggesting using the mysql result, but if you don't use the count in sql fetchAll() is efficient, if you save the result in a variable you won't loose a line.
$data = $dbh->query("SELECT * FROM ...");
$table = $data->fetchAll(PDO::FETCH_OBJ);
count($table)
will return the number of row and you can still use the result after like $row = $table[0]
or using a foreach
foreach($table as $row){
print $row->id;
}