I need to store the results from an SQL COUNT query in a PHP variable.
I know ModX uses PDO to run its SQL queries but I am unsure of how to go about creating this.
So far I have tried:
$sql = "SELECT COUNT (*) FROM `table` WHERE x = $x";
$results = $modx->query($sql);
$count = mysql_fetch_array($results);
echo $count;
and:
$sql = "SELECT COUNT (*) FROM `table` WHERE x = $x";
$results = $modx->getCount($sql);
echo $results;
but with no results.
Would anyone know the correct way to do this in ModX (Revo) and PDO?
A couple different methods
$results = $modx->query("select `username` from modx_users");
if (!is_object($results)) {
return 'No result!';
}
else {
$r = $results->fetchAll(PDO::FETCH_ASSOC);
echo count($r);
print_r($r);
}
$results = $modx->query("select count(id) as uids from modx_users where id >= '1'");
if (!is_object($results)) {
return 'No result!';
}
else {
$r = $results->fetch(PDO::FETCH_ASSOC);
echo 'uids = '.$r['uids'];
}