Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database Codigenator活动记录计数\u所有\u结果()_Database_Codeigniter - Fatal编程技术网

Database Codigenator活动记录计数\u所有\u结果()

Database Codigenator活动记录计数\u所有\u结果(),database,codeigniter,Database,Codeigniter,这是我的问题 $this->db->where( 'user_id',$this->session->userdata('id')); $this->db->from('votes'); $user_voted = $this->db->count_all_results(); 我想从表中获取用户记录,但要计算所有结果();不从表中计数。任何人告诉我答案。如果我的查询不正确。$this->db->count\u all\u results()替

这是我的问题

$this->db->where( 'user_id',$this->session->userdata('id'));
 $this->db->from('votes');
$user_voted = $this->db->count_all_results();

我想从表中获取用户记录,但要计算所有结果();不从表中计数。任何人告诉我答案。如果我的查询不正确。

$this->db->count\u all\u results()替换数据库调用中的$this->db->get()

$this->db->select('id');
$this->db->from('table');
$this->db->where($your_conditions);
$num_results = $this->db->count_all_results();
对于实际查询(您应该已经有):

或者您可以尝试以下方法:

$this->db->from(....);
$this->db->where(....);
$db_results = $this->get();

$results = $db_results->result();
$num_rows = $db_results->num_rows();
也可以尝试以下方法:

$id = $this->session->userdata('id');

$query = $this->db
                  ->select(column one, column two)
                  ->where('user_id',$id)
                  ->get('votes');
$rowcount = $query->num_rows();
试试这个代码

$this->db->select('COUNT(*) as total');
    $this->db->where('where', 0); 
   $query =  $this->db->get('table');

对于行或计数,类似以下内容:

$id=$this->session->userdata('id');
// num rows example
$this->db->select('*');
$this->db->where('user_id',$id);
$query = $this->db->get('votes');
$num = $query->num_rows();
// here you can do something with $query

// count all example
$this->db->where('user_id',$id);
$num = $this->db->count_all_results('votes');
// here you only have $num, no $query

试试这个:$num_results=$this->db->count_all_results()@ankitsuthar不工作。显示错误。$this->db->count\u all\u results()替换数据库调用中的$this->db->get()。此错误显示。错误号:1096未使用表选择*文件名:C:/xampp/htdocs/vote/system/database/DB_driver.php行号:691此行号无效。如下所述,只有COUNT()或COUNT(table.field)工作。我认为您需要指定在哪个表上使用COUNT(),因此将其更改为类似COUNT(vots.*)的内容。这可以解决问题,但如果数据集很大,也可能会比
COUNT
使用更多内存,但我看不到这种情况发生在这里。所以我们希望一切顺利。@ankitsuthar不会有结果。同样的错误显示。我不知道为什么查询不起作用。一切都是完美的。只是我从投票表中得到的结果表明该用户尚未投票。
$id=$this->session->userdata('id');
// num rows example
$this->db->select('*');
$this->db->where('user_id',$id);
$query = $this->db->get('votes');
$num = $query->num_rows();
// here you can do something with $query

// count all example
$this->db->where('user_id',$id);
$num = $this->db->count_all_results('votes');
// here you only have $num, no $query