Php 如何在ModX Revo中存储PDO/SQL计数结果?
我需要将SQL计数查询的结果存储在PHP变量中 我知道ModX使用PDO来运行其SQL查询,但我不确定如何创建它 到目前为止,我已经尝试:Php 如何在ModX Revo中存储PDO/SQL计数结果?,php,sql,pdo,modx,modx-revolution,Php,Sql,Pdo,Modx,Modx Revolution,我需要将SQL计数查询的结果存储在PHP变量中 我知道ModX使用PDO来运行其SQL查询,但我不确定如何创建它 到目前为止,我已经尝试: $sql = "SELECT COUNT (*) FROM `table` WHERE x = $x"; $results = $modx->query($sql); $count = mysql_fetch_array($results); echo $count; 以及: 但是没有结果 有人知道在ModX(Revo)和PDO中这样做的正确方法吗?
$sql = "SELECT COUNT (*) FROM `table` WHERE x = $x";
$results = $modx->query($sql);
$count = mysql_fetch_array($results);
echo $count;
以及:
但是没有结果
有人知道在ModX(Revo)和PDO中这样做的正确方法吗?有两种不同的方法
$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'];
}
你先说吧?我说了。我花了很多时间研究这个问题,尝试不同的东西。我找不到解决我问题的章节。getCount函数似乎处理数组,而不是单个查询结果。
$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'];
}