I’m currently in progress of updating all sql queries to pdo where I will use prepare statements to prevent sql injection attacks. So far so good, I’m stuck now in counting rows with pdo.
I tried following:
$sqlQuery = "SELECT COUNT(*) FROM News";
$STIH = $DBH->query($sqlQuery);
$rows_affected = $STIH->rowCount();
echo($rows_affected); // returning result is 1, it should be 1038
However, when I’m using old code like this
$query = "SELECT COUNT(*) AS numrows FROM News";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
echo ($numrows); // it return result which I expected which is 1038 rows.
What I am missing here.
Thanks
Use
$sth->fetchColumn()to grab the data.