[英]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.