Welcome Guest, Not a member yet? Register   Sign In
$query->num_rows(); // Value is not expected .. This is not the total value

How do I know the total number of rows from a query in which the query was performed as follows:

$query = $this->db->orderby(....);
$query = $this->db->get('table', 10, 30);
$query->num_rows(); // Value is not expected .. This is not the total value ...

If I do, it works, but I think there is a better way, even on account of the performance.
The way I'm using is creating a new variable ...

$query2 = $this->db->like('msg', 't');
$query2 = $this->db->get('table');
print "rows in Q". $query2->num_rows() // Expected value, but degraded performance for a table with many records.

num_rows() returns the number of rows in the resultset, not in the table. If you want that, use

Read the manual, it's excellent. This is documented.

You do not understand my question.
I have a query to the database, and I need the number of rows this query, but when using the parameter that references to "limit", example:

$query = $this->db->orderby (..., ...);
$query = $this->db->like ("...", ...);

$query = $this->db->get('mytable', 10, 20) // this
$query = $this->db->limit(10, 20) // or this

I can not get the total rows using:
$ query-> NUM_ROWS () / / will return 20 rows, because of the limit

My temporary solution was to create a new query to the database only to calculate the number of lines:

$query2 = $this->CI->db->like("user", ...);
$query2 = $this->CI->db->get('table');

The problem that the database has many records and many users access, it affects system performance. I wonder if another way to solve this.

You cannot do this in one query. You will have to do two queries: one without the limit to get the total rows, and one with the limit to limit your returned data.

EDIT: If doing two queries is affecting system performance, you might want to cache the total rows value. See Query Caching in the user guide.

function get_total_rows($table = '')
    $query = $this->db->get($table);
    return $this->query->num_rows();


Which is what I was trying to explain... Wink

Cache is good when it happens many actions in the database (insert, delete, update), and is not the case.

I decided a little better than the previous form, this time I do not need to create a variable "$query2" going all the information again, just enjoy the variable $query and a line do I make the call with the values ​​refernte to "limit" of course it still does not consider ideal.

The two temporary solution was as follows:

$query                    = $this->orderby(..., ...);
$query                    = $this->like(..., ...);
$query                    = $this->get(...');
$config['total_rows']     = $query->num_rows();        
$query                    = $this->get(..., ..., ...);
$data['results']          = $query->result();

u can use
$query = $this->db->orderby(....);

$total_row = $this->db->count_all_results('table');
$query = $this->db->get('table', 10, 30);

hope is ur Ans ^ ^


You have to use a combination of SQL_CALC_FOUND_ROWS and FOUND_ROWS(), which results in two queries.

Take a look at http://stackoverflow.com/questions/74218...mit-clause

Theme © iAndrew 2016 - Forum software by © MyBB