简体   繁体   English

使用查询中的count_all_results

[英]use count_all_results from query

i have complex query and say it $complexQuery, then i need to get all data row number from that without need the data result. 我有复杂的查询,并说它$ complexQuery,那么我需要从中获取所有数据行号,而无需数据结果。

I researched that count_all_results() i better than num_rows() 我研究过count_all_results()比num_rows()好

now say my code : 现在说我的代码:

$complexQuery = 'Some sql query';
$q = $this->db->query($complexQuery);
$total1 = $q->num_rows();

now i confuse to get all total data from that query, any suggestion for using $this->db->count_all_results() with that query ? 现在我混淆了从该查询中获取所有数据的任何建议,关于在该查询中使用$ this-> db-> count_all_results()的任何建议?

== SOLVED BY EDITING DB_active_rec.php == ==通过编辑DB_active_rec.php解决了==

i do this (leave as it is if tablename contained 'select') : 我这样做(如果表名包含“选择”,则保持原样):

public function from($from)
{
    foreach ((array)$from as $val)
    {
        if (strpos($val, ',') !== FALSE)
        {
            foreach (explode(',', $val) as $v)
            {
                $v = trim($v);
                $this->_track_aliases($v);
                $v = $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);

                if ($this->ar_caching === TRUE)
                {
                    $this->ar_cache_from[] = $v;
                    $this->ar_cache_exists[] = 'from';
                }
            }
        }
        else
        {
            $val = trim($val);

            // Added to bypass from arr if $val contained 'select' for complex query
            // $this->db->count_all_rows("select * from tableName")
            // will be select count(1) from (select * from tableName)
            if(FALSE !== strpos(strtolower($val),'select')){
                $this->ar_from[] = "($val)";
            }else{
                // Extract any aliases that might exist. We use this information
                // in the _protect_identifiers to know whether to add a table prefix
                $this->_track_aliases($val);
                $this->ar_from[] = $val = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
            }

            if ($this->ar_caching === TRUE)
            {
                $this->ar_cache_from[] = $val;
                $this->ar_cache_exists[] = 'from';
            }
        }
    }

    return $this;
}

that should be like: 应该是这样的:

$this->db->where($complexQuery);
$this->db->from('your_table_name');
echo $this->db->count_all_results();

See: Codeigniter count_all_results() 请参阅: Codeigniter count_all_results()

another minutes, but the same code 再过一分钟,但代码相同

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

      class SomeMyModel extends CI_Model {

      private static $db;

      function __construct() 
      {
        parent::__construct();
        self::$db = &get_instance()->db;
      }

        static function countTableResults(){
          self::$db->where($coplexQuery);
           return self::$db->count_all_results('#TABLENAME#');
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正确使用count_all_results()? - Appropriate use of count_all_results()? Codeigniter 随 `count_all_results` 改变 - Codeigniter changes with `count_all_results` 在CodeIgniter中验证count_all_results = 1 - Verify count_all_results = 1 in CodeIgniter Codeigniter count_all_results然后获取返回错误号1066 - Codeigniter count_all_results then get return error number 1066 如何在codeiginter中将count_all_results返回给ajax成功函数 - how to return count_all_results to ajax success function in codeiginter CodeIgniter中带有Active Record的“count_all_results”和“where”的问题 - problem with “count_all_results” and “where” with Active Record in CodeIgniter CodeIgniter 中的 $query&gt;num_rows() 和 $this-&gt;db-&gt;count_all_results() 之间的区别 &amp; 推荐哪一个 - difference between $query>num_rows() and $this->db->count_all_results() in CodeIgniter & which one is recommended 使用 $this-&gt;db-&gt;count_all_results() 时出错; 作为对布尔成员函数 num_rows() 的调用 - Error for using $this->db->count_all_results(); as Call to a member function num_rows() on boolean 计算db查询活动记录的所有结果 - count all results from a db query active records 如何计算查询结果中所有行的出现次数 - How to count all row occurrences of query results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM