Error when using SQL_CALC_FOUND_ROWS() - php

I'm doing a simple MySQL query to count the number of rows a query is returning without the effect of the LIMIT clause. I'm using Active Records with Codeigniter PHP framework.
Problem: I'm getting an error when SQL_CALC_FOUND_ROWS is used. Why is this so?
Query
SELECT `listing_id`, SQL_CALC_FOUND_ROWS listing_id FROM (`listings`) LIMIT 100
Error
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'SQL_CALC_FOUND_ROWS listing_id FROM (listings) LIMIT 100' at
line 1
Codeigniter Active Records
$this->db->select('listing_id')
->select('SQL_CALC_FOUND_ROWS listing_id', FALSE)
->from('listings')
->where('price < 1000')
->limit($limit, $offset)
->order_by('listing_id', 'desc');

SQL_CALC_FOUND_ROWS doesn't return a value, it's simply a modifier to indicate that the number of rows —where the LIMIT clause is not taken into account— should be saved so it can be retrieved later on, by using a second query (without generating the complete result set twice). Think of it the same as the DISTINCT keyword.
For more information, please read the documentation on this topic.

You can use MySql's count() function to achieve what you are looking for ie.
SELECT `listing_id`, count(*) FROM (`listings`) LIMIT 100

Related

SQL_CALC_FOUND_ROWS with FOUND_ROWS always returns 1

select SQL_CALC_FOUND_ROWS, col1 , mytable.id from mytable group by col1;
select found_rows();
select found_rows() , col1 , mytable.id from mytable group by col1;
This above query always results 1 for found rows. I am using php 7.1 and maria db 10.1 .
My backend is laravel 5.5 and I am making DB::raw request on mysql.
Server apache lamp server.
Is it a bug or there is some way around for this ?
Solution 1:
Please check the mysql version. This bug exists in 5.6.* and is solved in 5.6.11 release. Verify that your server has version different from [5.6.* < 5.6.11] to make this work.
This is a bug posted in mysql [not in mariadb] here:
http://bugs.mysql.com/bug.php?id=68458.
If you have verified the version of mysql, then check below explanation:
NOTE: It is mentioned in the bug link that "MySQL server compiled from "5.6.11-log Source distribution" still has this bug."
Solution 2:
found_rows() and limit in the same query will not result in total count but will give count for data with limit.
If there is a limit in the select query, you will have to use
"select SQL_CALC_FOUND_ROWS, ....... limit x;"
and immediately next execute
"select found_rows();"
OR
If there is no limit in the select statement then you can directly execute
"select found_rows(), .......;"
Explanation here: https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_found-rows
The example query seems to be wrong, as you should have the col1 to be a part of selected fields
found_rows returns the number of rows found in the previous query.
Have you checked the result of the first query? how many rows it returns?
If it returns more than 1 rows - meaning that some query is being executed between your query and select found_rows() at the same session.
I didn't succeed to reproduce this problem, but you can try using transaction here.
On the other part -
From the query it seems that you are trying to get a distinct count from a table, but using a group by for some reason.
did you try using
select count( disctinct col1) from mytable
If this does not work for you, your second best option is:
select count(1) from (<your query goes here>)

ORDER BY FIELD does not work with php

I need the MySQL parameter ORDER BY FIELD to order threads from a forum.
Here the exact SQL command: $ids is "19,3,12,256,1023"
SELECT*
FROM
Threads
WHERE
ID IN ($ids)
LIMIT
$untere_schranke, 50
ORDER BY FIELD(ID,$ids)
In phpMyAdmin it works fine but when I use php i get this:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'ORDER BY FIELD(ID, 19,3,12,256,1023)' at line 15
Order by statement should be before the limit
order by FIELD(ID,$ids)
LIMIT
$untere_schranke, 50

Wrong syntax (MySQL, PHP)

This line of code
$SQL = "SELECT * FROM stats ORDER BY Team WHERE Team='$teamval'";
is returning with the following MySQL error:
You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'WHERE Team='OTT''
at line 1
I can't find anything wrong with the syntax, what's wrong with it?
The ORDER BY clause must appear after the WHERE clause. So, your query should, instead, be:
SELECT * FROM stats WHERE Team='$teamval' ORDER BY Team
You have used in correct syntax of using order by before where clause , ORDER BY should be used at the end of query if you have used limit in your query then put order by before limit
SELECT * FROM stats WHERE Team='$teamval' ORDER BY Team
Replace:
$SQL="SELECT * FROM stats WHERE Team='$teamval' ORDER BY Team";

MySqli and PHP: Not in statement

I'm trying to get data by an mysqli query.
Query looks like:
SELECT * FROM pxldr_drawings
ORDER BY RAND()
WHERE id NOT IN (1,3,4,2)
LIMIT 1
But i get the following error message
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id NOT IN (1,3,4,2) LIMIT 1' at line 3
I also tried NOT IN ('1','3','4','2') and NOT IN(1,3,4,2), but neither worked.
Thanks, LB
WHERE clause must be before ORDER BY clause.
try this
SELECT * FROM pxldr_drawings
WHERE id NOT IN (1,3,4,2)
ORDER BY RAND()
LIMIT 1
The order of clauses is important, order by should come after where.
SELECT * FROM pxldr_drawings
WHERE id NOT IN (1,3,4,2)
ORDER BY RAND()
LIMIT 1
For the proper syntax of where different clauses need to be placed, please refer to "Select Syntax" documentation.

SQL/PHP query error

I have the following error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select manufacturers_id, manufacturers_name, manufacturers_image, date_added, la' at line 1
The query:
SELECT COUNT(
SELECT manufacturers_id,
manufacturers_name,
manufacturers_image,
date_added,
last_modified
FROM manufacturers
ORDER BY manufacturers_name
) AS total
This happens also to some other categories in my website. All finish their line incomplete.
This query would do what you want:
SELECT COUNT(*) AS total
FROM manufacturers
What's wrong with your query:
COUNT() accepts an expression or single column as parameter - you're passing multiple
ORDER BY doesn't make much sense as soon as what you need is just number of rows (for obvious reason that number doesn't depend on order)
Maybe because the code is not on here full but what I can see is the mysql/mysqli_query is missing, and the ";" at the end of the code.

Categories