php codeigniter 计数行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28958307/
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-26 00:10:24  来源:igfitidea点击:

php codeigniter count rows

phpcodeigniter

提问by Nilab

I am new in codeigniter, I want to count all rows from database table but the query dose not retrieve the exact total of rows. Here is Model

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

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

and this is the controller

这是控制器

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

this is the view

这是视图

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

回答by marcosh

You could use the helper function $query->num_rows()

您可以使用辅助功能 $query->num_rows()

It returns the number of rows returned by the query. You can use like this:

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

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

回答by Deepak singh Thakur

Use this code:

使用此代码:

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

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

or

或者

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

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

回答by user1587985

If you really want to count all rows. You can use this in model function:

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

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

It returns single value, that is row count

它返回单个值,即行数

回答by TechMaestro

This is what is did that solved the same problem. I solved it by creating a function that returns the query result thus:

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

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

//The above code can go in the user_model or whatever your model is.

//上面的代码可以放在 user_model 或任何你的模型中。

This allows me to use one function for the result and number of returned rows.

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

Use this code below in your contoller where you need the count as well as the result array().

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

//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();

I hope this helps.

我希望这有帮助。

回答by Avigit.M

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

回答by jual ahmed

public function record_count() {
   return $this->db->count_all("tablename");
}

回答by Kelvin Barsana

replace the query inside your model function with this

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

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

in view:

鉴于:

echo $query->num_rows();

回答by Hariharan.P

You may try this one

你可以试试这个

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

回答by Umair Sultan

To count all rows in a table:

要计算表中的所有行:

echo $this->db->count_all_results('table_name');

To count selected rows from table:

要从表中计算选定的行:

echo $this->db->where('name', $name)->count_all_results('table_name');

回答by Dilpreet

To count all rows in a table:

要计算表中的所有行:

Controller:

控制器:

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

Model:

模型:

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