SELECT COUNT (*) FROM `table` WHERE x = $x;
$sql = "SELECT COUNT (*) FROM `table` WHERE x = $x"; $results = $modx->query($sql); $count = mysql_fetch_array($results); echo $count;
$sql = "SELECT COUNT (*) FROM `table` WHERE x = $x"; $results = $modx->getCount($sql); echo $results;
This question has been answered by zimbolite. See the first response.
Try this...
$sql = "SELECT COUNT(*) as num FROM modx_users";
$result = $modx->query($sql);
if (!is_object($result)) {
return 'No result!';
}
else {
$row = $result->fetch(PDO::FETCH_ASSOC);
$numResults = $row['num'];
return 'Result:' . $numResults;
}
$c = $modx->newQuery('modUser');
$c->where(array(
'username:LIKE' => 'a%'
));
echo $modx->getCount('modUser', $c);
I've just tried it, and it worked for me. What is the PHP error you get?
Since you querying a MODX table, Gary Nutting's method is much easier and more reliable.
This may help as well: http://bobsguides.com/revolution-objects.html
$results = $modx->query("select count(items) as myitems from customtable where id >= '1'");
if (!is_object($results)) {
return 'No result!';
}
else {
$r = $results->fetch(PDO::FETCH_ASSOC);
echo 'uids = '.$r['myitems'];
}