php 计算Codeigniter中数据库查询返回的结果数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3667715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:38:15  来源:igfitidea点击:

Counting the number of results returned by a database query in Codeigniter

phpcodeigniterdatabase-abstraction

提问by Linker3000

I am not having much luck detecting when a database query in Codeigniter returns zero results. I have had a good read of the notes on the PHP count function but am none the wiser!

当 Codeigniter 中的数据库查询返回零结果时,我没有太多运气检测。我已经很好地阅读了有关 PHP count 函数的注释,但我并不聪明!

I call the query/view as follows from the controller:

我从控制器调用查询/视图如下:

$data['result'] = $this->search_model->do_search(set_value('name'));
$data['title'] = "Search results";
$this->load->view('search_view',$data);

The view generates a results table for me OK, but when I try and trap an empty result, the count always returns 1:

该视图为我生成了一个结果表 OK,但是当我尝试捕获一个空结果时,计数​​总是返回 1:

I have tried if count(array($result))and just if count($result)

我已经尝试过if count(array($result))并且只是if count($result)

So what's a good way to get the count? I'm using Fedora 13 with PHP 5.3.3 on my dev laptop.

那么获得计数的好方法是什么?我在我的开发笔记本电脑上使用 Fedora 13 和 PHP 5.3.3。

回答by janosrusiczki

Have a look at $query->num_rows(<- clickable).

看看$query->num_rows(<-可点击)。

回答by janosrusiczki

The best thing to do in your model is the following:

在您的模型中最好的做法如下:

$query = $this->db->something()....
...
...
if ( $query->num_rows() > 0 )
{
    return $query->result();
}
else
{
    return FALSE;
}

Then in your controller or view you would do the following:

然后在您的控制器或视图中,您将执行以下操作:

if ( !empty($my_db_result) ) 
{
    ......
}

This process enables you to respond on the result based on the result type. If the rows could be retrieved this will return an array of which the items can be counted by PHP's count() function. Since the second block checks if the result is empty (note that "FALSE" is treated as being empty) you won't bump into any issues (e.g. when using a foreach loop) and you can specify what to do in case there were no results.

此过程使您能够根据结果类型对结果做出响应。如果可以检索行,这将返回一个数组,其中的项目可以通过 PHP 的 count() 函数进行计数。由于第二个块检查结果是否为空(请注意,“FALSE”被视为空),因此您不会遇到任何问题(例如,在使用 foreach 循环时)并且您可以指定在没有的情况下要做什么结果。

回答by rabidmachine9

Try if(isset($result) && count($result))on your view file then inside the if statement you can write the code you want to executed when the inserts in your db are more than 0...good luck!

试试if(isset($result) && count($result))你的视图文件,然后在 if 语句中,你可以编写当你的数据库中的插入超过 0 时你想要执行的代码......祝你好运!

回答by Tejas Patel

If you put count($result)in ifstatement then when it succeds, it returns only 1.

如果你把count($result)if声明中那么当它succeds,它只返回1

You can try $query->num_rows()in a different way.

你可以换$query->num_rows()一种方式尝试 。

回答by casti cyber dev

for example i count to show admins connected ans users connected

例如,我计数显示已连接管理员和已连接用户

Database

数据库

in users i add table : [status] [int] [1]

在用户中我添加表:[status] [int] [1]

in users also i have : [role] [varchar] [255]

在用户中,我也有:[角色] [varchar] [255]

Update statut to 1 (onligne) in login validation()

在登录验证()中将 statut 更新为 1(onligne)

if($this->model_users->can_log_in($email,$pass)){   
$update = array('status' => 1);
$this->model_users->update_onligne($email,$update);
redirect('main/members');

and the model :

和模型:

public function update_onligne($email,$update){
        $this->db->where('email',$email);
        $this->db->update('users',$update);
        return true;
    }

Update status to offline in logout()

在 logout() 中更新状态为离线

Logout controller :

注销控制器:

public function logout(){
        $id = $this->session->userdata('id');
        $update = array('status' =>0);
        $this->model_users->logout($id,$update);
        $this->session->sess_destroy();
        redirect('main/login');
    }

Logout model :

登出模式:

public function logout($id,$update){
        $this->db->where('id',$id);
        $this->db->update('users', $update);
        return;
    }

Count Onligne :

在线计数:

The Controller :

控制器:

$data['admin_onligne'] = $this->model_users->count_onligne_admin();
$data['user_onligne'] = $this->model_users->count_onligne_users();
$this->load->view('template/page_left',$data);

The Model :

该模型 :

public function count_onligne_admin(){
            $query = $this->db->query('SELECT COUNT(status) AS enligneadmin FROM users WHERE status=1 AND role="admin"')->row_object();
            return $query->enligneadmin;
        }

public function count_onligne_users(){
            $query = $this->db->query('SELECT COUNT(status) AS enligneuser FROM users WHERE status=1 AND role="etudiant"')->row_object();
            return $query->enligneuser;
        }

The Viewer

观众

<span><?php echo $user_onligne ;?> User en ligne</span>
<span><?php echo $admin_onligne ;?> Admin en ligne</span>

回答by casti cyber dev

i'm dowing this and it workd for me

我正在做这个,它对我有用

Controller

控制器

$id = $this->session->userdata('id');
if($this->model_users->if_user_dont_have_email($id)){
....
}

Model Users

示范用户

public function if_user_dont_have_email($id){
    $query = $this->db->query("SELECT email FROM users WHERE id='$id'");
    if ($query->num_rows() > 0) {
        $row = $query->row_array(); 
        if(empty($row['email'])){
            return true;
        }else{
            return false;
        }
    }
}