Php 计数行数问题

Php 计数行数问题,php,codeigniter,Php,Codeigniter,我有这样一个问题: SELECT * FROM configurations WHERE ID = userID if ($query->num_rows() > 0) { foreach($query->result() as $row) { // display data } else { // display no data found message } 我有这样的逻辑: SELECT * FROM configurations WHERE ID = us

我有这样一个问题:

SELECT * FROM configurations WHERE ID = userID
if ($query->num_rows() > 0) {

foreach($query->result() as $row) { 
// display data
}

else {

// display no data found message

}
我有这样的逻辑:

SELECT * FROM configurations WHERE ID = userID
if ($query->num_rows() > 0) {

foreach($query->result() as $row) { 
// display data
}

else {

// display no data found message

}
但当我尝试这样做时:

count($query->num_rows())

无论返回多少个结果,它始终为1。如果返回1个或多个,则执行“如果”。如果没有返回结果,则执行“else”。然而,计数从未改变。发生了什么事?

首先
否则{
应该是
}否则{
-你缺少一个大括号

其次,
count()
用于计算数组中的元素数。由于
$query->num\u rows()
返回的是一个数字而不是数组,因此不需要使用
count()
。在您的例子中,count()告诉您有一个数字,而不是实际的数字

尝试:


首先,
else{
应该是
}else{
-你缺少一个大括号

其次,
count()
用于计算数组中的元素数。由于
$query->num\u rows()
返回的是一个数字而不是数组,因此不需要使用
count()
。在您的例子中,count()告诉您有一个数字,而不是实际的数字

尝试:


您将通过$query->num_rows()获得一个整数,因此当您通过count()函数运行该整数时,它被设计为返回1

如果var不是数组或对象 实现了可数接口, 将返回1个。有一个 异常,如果var为NULL,则0将为 返回


您将通过$query->num_rows()获得一个整数,因此当您通过count()函数运行该整数时,它被设计为返回1

如果var不是数组或对象 实现了可数接口, 将返回1个。有一个 异常,如果var为NULL,则0将为 返回

您不能
count()
一个数字,它可以正常工作。如果您只需要表中的记录数,请在SQL中使用
count()

$sql = mysql_query("SELECT COUNT(0) AS count FROM configurations WHERE ID = userID");
$sql = mysql_fetch_assoc($sql);
$count = $sql["count"];
否则,只需按照@chriso所述分配
$count=$query->num_rows();

您不能
count()
一个数字,它可以正常工作。如果您只需要表中的记录数,请在SQL中使用
count()

$sql = mysql_query("SELECT COUNT(0) AS count FROM configurations WHERE ID = userID");
$sql = mysql_fetch_assoc($sql);
$count = $sql["count"];
否则,只需按照@chriso所述分配
$count=$query->num_rows();

num_rows()返回单个数值(行数仅为单个数字)而不是数组,因此count()只有一个值可供计数…num_rows()是结果数num_rows()返回单个数值(行数只是一个数字)不是数组,因此count()只有一个值可以计数…num_rows()是结果数