Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在ModX Revo中存储PDO/SQL计数结果?_Php_Sql_Pdo_Modx_Modx Revolution - Fatal编程技术网

Php 如何在ModX Revo中存储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计数查询的结果存储在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中这样做的正确方法吗?

有两种不同的方法

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


}