Monday, January 21, 2013

How to get row count from MySQL with PHP/PDO

PDOStatement::rowCount() does not work well with MySQL, so here is the workaround code:
$pdo = new PDO('mysql:dbname=database;host=localhost;charset=UTF-8', 'username', 'password',
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
...
$total = $pdo->query('SELECT COUNT(*) FROM database')->fetchColumn();

No comments:

Post a Comment