【问题标题】:How to store results of PDO/SQL Count in ModX Revo?如何在 ModX Revo 中存储 PDO/SQL 计数的结果?
【发布时间】:2014-02-10 23:46:24
【问题描述】:

我需要将 SQL COUNT 查询的结果存储在 PHP 变量中。

我知道 ModX 使用 PDO 来运行其 SQL 查询,但我不确定如何创建它。

到目前为止我已经尝试过:

$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;

但没有结果。

有人知道在 ModX (Revo) 和 PDO 中执行此操作的正确方法吗?

【问题讨论】:

  • 你从reading the fine manual开始怎么样?
  • 我做到了。我花了很多时间来研究这个并尝试不同的事情。我找不到解决我的问题的部分。 getCount 函数似乎处理的是数组,而不是单个查询结果。

标签: php sql pdo modx modx-revolution


【解决方案1】:

几种不同的方法

$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'];


}

【讨论】:

    猜你喜欢
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    相关资源
    最近更新 更多