Alternative for mysql_num_rows using PDO

$res = $DB->query('SELECT COUNT(*) FROM table');
$num_rows = $res->fetchColumn();

or

$res = $DB->prepare('SELECT COUNT(*) FROM table');
$res->execute();
$num_rows = $res->fetchColumn();

You can use this to ask if data exists or is selected, too:

$res = $DB->query('SELECT COUNT(*) FROM table');
$data_exists = ($res->fetchColumn() > 0) ? true : false;

Or with your variables:

$res = $DB->query('SELECT COUNT(*) FROM table');
$message = ($res->fetchColumn() > 0) ? array('status' => 'ok') : array('status' => 'error');

Leave a Comment