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

[eluser]soprabaixar1[/eluser]
How do I know the total number of rows from a query in which the query was performed as follows:

Code:
$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 ...

Code:
$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.
#2

[eluser]WanWizard[/eluser]
num_rows() returns the number of rows in the resultset, not in the table. If you want that, use
Code:
$this->db->count_all('table');

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

[eluser]soprabaixar1[/eluser]
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:

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

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

I can not get the total rows using:
Code:
$ 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:

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

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

[eluser]wh1tel1te[/eluser]
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.
#5

[eluser]InsiteFX[/eluser]
Code:
function get_total_rows($table = '')
{
    $query = $this->db->get($table);
    return $this->query->num_rows();
}

InsiteFX
#6

[eluser]WanWizard[/eluser]
Which is what I was trying to explain... Wink
#7

[eluser]soprabaixar1[/eluser]
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:

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

[eluser]meemeo[/eluser]
u can use
Code:
$query = $this->db->orderby(....);

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

hope is ur Ans ^ ^


#9

[eluser]gRoberts[/eluser]
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