php - Row count with PDO -


there many conflicting statements around. best way row count using pdo in php? before using pdo, used mysql_num_rows.

fetchall won't want because may dealing large datasets, not use.

do have suggestions?

$sql = "select count(*) `table` foo = bar";  $result = $con->prepare($sql);  $result->execute();  $number_of_rows = $result->fetchcolumn();  

not elegant way it, plus involves query.

pdo has pdostatement::rowcount(), apparently not work in mysql. pain.

from pdo doc:

for databases, pdostatement::rowcount() not return number of rows affected select statement. instead, use pdo::query() issue select count(*) statement same predicates intended select statement, use pdostatement::fetchcolumn() retrieve number of rows returned. application can perform correct action.

edit: above code example uses prepared statement, in many cases unnecessary purpose of counting rows, so:

$nrows = $pdo->query('select count(*) blah')->fetchcolumn();  echo $nrows; 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -