diferencia entre query query>num rows() y this this->db->count all results() en CodeIgniter & cuál se recomienda


En un escenario necesito saber el recuento de conjunto de registros que devolverá una consulta, que en codeigniter se puede hacer mediante $query->num_rows() o $this->db->count_all_results(). ¿Cuál es mejor y cuál es la diferencia entre estos dos?

Author: Narendrasingh Sisodia, 2011-08-12

8 answers

Con num_rows() primero realiza la consulta, y luego puede verificar cuántas filas tiene. count_all_results() por otro lado, solo le da el número de filas que produciría su consulta, pero no le da el conjunto de resultados real.

// num rows example
$this->db->select('*');
$this->db->where('whatever');
$query = $this->db->get('table');
$num = $query->num_rows();
// here you can do something with $query

// count all example
$this->db->where('whatever');
$num = $this->db->count_all_results('table');
// here you only have $num, no $query
 40
Author: danneth,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-12 07:49:12

$this->db->count_all_results es parte de una consulta Active Record (preparando la consulta, para devolver solo el número, no los resultados reales).

$query->num_rows() se realiza en un objeto resultset (después de devolver los resultados de la base de datos).

 5
Author: Jens Roland,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-12 07:43:42

Which one is better and what is the difference between these two Es casi imposible para mí, alguien solo quiere obtener el número de registros sin volver a tocar o realizar otra consulta que involucró mismo recurso. Además, la memoria utilizada por estas dos funciones es de la misma manera después de todo, ya que con count_all_result todavía realizas get (en términos de CI AR), por lo que te recomiendo usar la otra (o usar count() en su lugar) que te dio beneficios de reutilización.

 2
Author: toopay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-12 11:59:20

Hay dos formas de contar el número total de registros que la consulta devolverá. Primero esto

$query = $this->db->query('select blah blah');  
return $query->num_rows();

Esto devolverá el número de filas que la consulta trajo.

Segundo

return $this->db->count_all_results('select blah blah');

Simplemente count_all_results requerirá ejecutar la consulta de nuevo.

 2
Author: Muhammad Raheel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-03 11:04:29

También podemos usar

return count_all('table_name');  

O

$this->db->from('table_name');
return $this->db->count_all_result();

O

$query = $this->db->query('select * from tab');  
return $query->num_rows();
 1
Author: Amit Joshi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-12 11:14:43

Número total de resultados

$this->db->count_all_results('table name');
 0
Author: Deepak singh Thakur,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-02 06:37:08

Simplemente como abajo;

$this->db->get('table_name')->num_rows();

Esto obtendrá el número de filas/registros. sin embargo, también puede usar parámetros de búsqueda;

$this->db->select('col1','col2')->where('col'=>'crieterion')->get('table_name')->num_rows();

Sin embargo, debe tenerse en cuenta que verá errores malos si se aplica de la siguiente manera;

$this->db->get('table_name')->result()->num_rows();
 0
Author: Shariati,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-19 10:56:45
$sql = "select count(*) as row from login WHERE firstname = '" . $username . "' AND password = '" . $password . "'";
    $query = $this->db->query($sql);
    print_r($query);exit;
    if ($query->num_rows() == 1) {
    return true;
    } else {
    return false;
    }
 0
Author: Anandavel A,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-25 11:50:37