【问题标题】:php codeigniter count rows [duplicate]php codeigniter计数行[重复]
【发布时间】:2015-05-11 13:42:54
【问题描述】:

我是 codeigniter 的新手,我想计算数据库表中的所有行,但查询不会检索到确切的总行数。 这是模型

public function countRow(){
$query = $this->db->query("SELECT *,count(id) AS num_of_time FROM home");
    // print_r($query->result());
    return $query->result();
}

这是控制器

public function countTotalrow(){
     $data['query'] = $this->home_model->countRow(); 
}

这是风景

foreach ($num_of_time as $row){
    <span><?php print_r($row->id);?></span>

【问题讨论】:

  • 如果它没有得到确切的总行数,它会检索什么?
  • 我认为第三个代码示例中的变量 '$num_of_time' 应该是 '$query'
  • 它给出了不正确的数字 11,但表中的记录是 14。
  • 尝试用这个“print_r($row->num_of_time)”替换这个“print_r($row->id)”
  • 当我将 "print_r($row->id)" 更改为 "print_r($row->num_of_time)" 时不工作,它得到 21。

标签: php codeigniter


【解决方案1】:

你可以使用辅助函数$query-&gt;num_rows()

它返回查询返回的行数。你可以这样使用:

$query = $this->db->query('SELECT * FROM my_table');
echo $query->num_rows();

【讨论】:

  • 当数据库引擎只能从其元数据存储中返回总行数时,没有理由运行完整查询。它是资源浪费并且不能很好地扩展。不要查询整个表,而是使用$this-&gt;db-&gt;count_all_results()
  • 是的,我同意@pbarney。没有必要触发完整查询,因为您可以从 $this->db->from('home'); 获取记录$this->db->count_all_results();来源:codeigniter.com/user_guide/database/…
【解决方案2】:

使用此代码:

$this-&gt;db-&gt;where(['id'=&gt;2])-&gt;from("table name")-&gt;count_all_results();

$this-&gt;db-&gt;from("table name")-&gt;count_all_results();

【讨论】:

    【解决方案3】:

    你可以试试这个

        $this->db->where('field1',$filed1);
        $this->db->where('filed2',$filed2);
        $result = $this->db->get('table_name')->num_rows();
    

    【讨论】:

      【解决方案4】:

      如果你真的想计算所有行。您可以在模型函数中使用它:

      $this->db->select('count(*)');
      $query = $this->db->get('home');
      $cnt = $query->row_array();
      return $cnt['count(*)'];
      

      返回单个值,即行数

      【讨论】:

        【解决方案5】:

        这就是解决相同问题的方法。我通过创建一个返回查询结果的函数来解决它:

        function getUsers(){
        $query = $this->db->get('users');
        return $query->result();
        }
        

        //上面的代码可以放在user_model或者你的model里面。

        这允许我对结果和返回的行数使用一个函数。

        在您需要计数和结果数组()的控制器中使用下面的代码。

        //This gives you the user count using the count function which returns and integer of the exact rows returned from the query.
        $this->data['user_count'] = count($this->user_model->getUsers());
        
        //This gives you the returned result array.
        $this->data['users'] = $this->user_model->getUsers();
        

        我希望这会有所帮助。

        【讨论】:

          【解决方案6】:
          public function record_count() {
             return $this->db->count_all("tablename");
          }
          

          【讨论】:

            【解决方案7】:

            用这个替换模型函数中的查询

            $query = $this->db->query("SELECT id FROM home");
            

            在视图中:

            echo $query->num_rows();
            

            【讨论】:

            • 它给出了这个错误遇到 PHP 错误严重性:警告消息:为 foreach() 提供的参数无效
            • @nilab 不包括 foreach,只包括回声
            • 不工作它显示错误的数字@Kelvin Barsana 我需要我的表格的总条目数,即 14 删除 foreach 后它给了我 21。
            • 模型:$query = $this->db->query('SELECT id FROM idea_home');返回$查询;查看:num_rows();?>
            • num_rows() 显示已经获取了多少行,所以看到不同的结果有点奇怪,将查询更改为 'SELECT *' 怎么样
            【解决方案8】:

            统计表中的所有行数:

            控制器:

            function id_cont() {
               $news_data = new news_model();
               $ids=$news_data->data_model();
               print_r($ids);
            }
            

            型号:

            function data_model() {
                $this->db->select('*');
                $this->db->from('news_data');
                $id = $this->db->get()->num_rows();
                return $id;
            }
            

            【讨论】:

              【解决方案9】:
              public function number_row()
               {
                  $query = $this->db->select("count(user_details.id) as number")
                                     ->get('user_details');              
                    if($query-> num_rows()>0)
                    {
                        return $query->row();
                    } 
                    else
                    {
                        return false;
                    } 
                 }
              

              【讨论】:

                【解决方案10】:

                试试这个:) 我创建了计算所有结果的模型

                在库模型中

                function count_all_results($column_name = array(),$where=array(), $table_name = array())
                {
                        $this->db->select($column_name);
                        // If Where is not NULL
                        if(!empty($where) && count($where) > 0 )
                        {
                            $this->db->where($where);
                        }
                        // Return Count Column
                        return $this->db->count_all_results($table_name[0]);//table_name array sub 0
                }
                

                您的控制器将如下所示

                public function my_method()
                {
                  $data = array(
                     $countall = $this->model->your_method_model()
                  );
                   $this->load->view('page',$data);
                }
                

                然后在你的模型中简单地调用库模型

                function your_method_model()
                {
                        return $this->library_model->count_all_results(
                               ['id'],
                               ['where],
                               ['table name']
                           );
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-06-19
                  • 1970-01-01
                  • 2018-09-26
                  • 2012-02-25
                  相关资源
                  最近更新 更多