counting all rows and getting rows in codeigniter - php

I do two queries, one to get the count of all results and one to get the actual results 9 by 9.
my problem is when i try to get the count of the results the second query get all the rows.
$courses = $this->load->model("course")->where("deleted",0);
$courses->where("country",strtolower($country));
$courses->count(); // OK
$courses->offset(($per_page)*9)->limit(9);
$courses->get(); // returns 9 rows from all database (like select * from courses limit 9) without where country=france
my problem is I want to count all the results but I want to get just 9 results for pagination purposes

Need detail of your problem you can check the last executed query by running
echo $this->db->last_query();
And then you will be able to trace it.

Assuming that:
$courses = $this->db;
$course->get() will give you full record. If you want count do this:
$query = $course->get();
echo $query->num_rows();//will give you row count
Note: If you want to get all records also you have you use two
separate query for fetching records and getting query.
I would suggest you to use Codeigniter Pagination Class

Related

MySQL - PHP - Avoiding multiple queries to count results

I am trying to get some results for a query and count all the data results using PHP.
Actually I'm doing something like this:
$MQ=$cnx->query("SELECT COUNT(id) FROM table WHERE field=1;");
$MFA=$cnx->fetch_row();
$count=$MFA[0];
echo "Showing $count results";
$MQ=$cnx->query("SELECT id,name FROM table WHERE field=1;");
while($MFA=$MQ->fetch_assoc()){
// show something with $MFA[id] and $MFA[name]
}
I want to avoid using 2 queries, Is there any way to get the rows count and then get all the results without using an array?
You can use the num_rows property to get the number of rows in a result set.
$MQ=$cnx->query("SELECT id,name FROM table WHERE field=1;");
$count=$MQ->num_rows;
echo "Showing $count results";
while($MFA=$MQ->fetch_assoc()){
// show something with $MFA[id] and $MFA[name]
}
Is there any way to get the rows count and then get all the results without using an array?
There is, but you don't need it.
In fact, you should use an array, to separate your business logic from presentation logic.
And as long as you have an array, you can always count it's members.
You definitely should quit the practice of showing the query results right during the query execution. Instead, learn logic separation and use of templates.

Adding count(*) to existing mysql query causes unwanted action in php

$results_ts = mysqli_query($connection, "SELECT id, title, description, content
FROM prose WHERE title LIKE '%$search_title%' LIMIT 10");
if(isset ($_GET['search_title'])){
while($title_arr = mysqli_fetch_array($results_ts)){
echo $title_arr[id]; //and so on...
And that works as I wanted.
but when I try to add , count(*) after where content is, than while() loop echoes only one result, even when $title_arr[4](which stands for count) results in many.
Is it possible to do it this sort of way, or should I be running two separate queries?
You shouldn't do that in one query.
Think about the result you're expecting: when you do a count(*) query, what you get back is a result with only one row and one value. If you select multiple lines, then what should it do? Inject the count into each row of the data?
From a logical perspective, those are two different things you're looking for.
EDIT: counting the rows in your result after the fact is probably the best option, but that won't work if you're only looking for the first 10 entries, and there's no reason to SELECT the whole table if you don't need all the data. That's why count(*) is fulfilling such a different role.
COUNT() is an aggregate function so you can't use it like this. But if all you want is the number of rows this query would have returned you can use SQL_CALC_FOUND_ROWS to get the number of rows that would have been returned if thee was no LIMIT clause. You then can get the results by calling SELECT FOUND_ROWS() after your query is run.

Counting rows in mysql query

I'm trying to count the number of rows that are returned to $data in a wordpress database query that I'm making. See below
$data = $wpdb->get_results($wpdb->prepare("SELECT * FROM table WHERE wordpress_id=%d",get_current_user_id()),ARRAY_A);
The issue that I seem to be running into is the use of a prepared statement. I figure I could just write a loop to go through the array and tally them up but is there a more elegant way of doing this?
Many thanks in advance.
If you only need the count, change your SQL query to SELECT COUNT(*) as count .... This is efficient since the counting will be done on the database itself, and you won't be fetching all the data through the network.
If, however, you need both the data and the count, fetch the data as you are, and get the count by doing count($data).
Reference: count()

Use PHP while() only to return the first few values.

I am using PHP while() to fetch the results of a database query.
while($results = mysql_fetch_array( $galleryresult )) { // show the results };
However, I would like to paginate the results if they exceed a certain number of entries. So for example if there are more than 12 entries returned, only the first 12 will be shown.
1) How can I adapt while() so only the first 12 entries are returned on the first page.
2) Then how can I adapt while() so the 13th-24th result are returned on the second page and so on.
Modify the query such that only 12 entries are in the result set. That way you do not need to modify the while loop.
Add a parameter to the URL, that tells the script the offset that should be used in the SQL query.
Create a query that counts the total number of results. Use this to determine how many pages should be displayed in the pager.

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query?
None of the fetch statements that I've found return a 2 dimensional array to access specific rows.
I want to be able to return only 1 specific row, kinda like mysql_result... except for entire row instead of 1 cell in a row.
I don't want to loop through all the results either, I already know how to do that I just thought that there might be a better way that I'm not aware of. Thanks
For example, mysql_data_seek() and mysqli_stmt_data_seek() allow you to skip forward in a query result to a certain row.
If you are interested in one certain row only, why not adapt the query to return only the row you need (e.g. via a more specific WHERE clause, or LIMIT)? This would be more resource-effective.
You should add LIMIT to your mysql statement. And it will return only data you need. Like following:
-- returns 1 row after 2 row
SELECT * FROM table LIMIT 2, 1

Categories