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

发布于 2024-09-18 05:22:37 字数 453 浏览 5 评论 0原文

我没有太多运气来检测 Codeigniter 中的数据库查询何时返回零结果。我已经仔细阅读了有关 PHP 计数函数的注释,但我仍然一无所知!

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

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

视图为我生成一个结果表,好的,但是当我尝试捕获空结果时,计数​​始终返回 1:

我尝试过 if count(array($ result)) 和 if count($result)

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

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!

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:

I have tried if count(array($result)) and just 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

左耳近心 2024-09-25 05:22:37

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

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

柳若烟 2024-09-25 05:22:37

在模型中最好执行以下操作:

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

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

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

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

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.

淡淡離愁欲言轉身 2024-09-25 05:22:37

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

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!

夜光 2024-09-25 05:22:37

如果您将 count($result) 放在 if 语句中,那么当成功时,它仅返回 1

你可以尝试
$query->num_rows() 以不同的方式。

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

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

挖鼻大婶 2024-09-25 05:22:37

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

数据库

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

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

< strong>在登录验证()中将状态更新为1(在线)

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

和模型:

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

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

注销控制器:

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');
    }

注销模型:

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

在线计数:

控制器:

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

模型:

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

查看器< /强>

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

for example i count to show admins connected ans users connected

Database

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

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

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

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 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>
雨后咖啡店 2024-09-25 05:22:37

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

控制器

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

模型用户

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

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文