-2

I have some code here that gets the nth row from db. Problem is that it sometimes skips a row or few rows or just misses it. The f_id is set to auto increment with no gaps. Regardless it should not skip any f_id, what am doing wrong?

$sql2 = mysql_query("SELECT *
FROM (
    SELECT
        @row := @row +1 AS rownum, price,f_id, car, color
    FROM (
        SELECT @row :=0) r, $tbName WHERE (car= '$car') $color $location AND (price BETWEEN '".$min."' AND '".$max."')
    ) ranked
WHERE rownum %25 =1 order by price ASC, f_id ASC");

I noticed that the WHERE section does not work properly when using the OR exp AND (color = 'green' OR color = 'red') it sometimes skips the red color. Any ideas?

Matt
  • 7
  • 4
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 26 '15 at 15:16
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Aug 26 '15 at 15:16
  • so what you guys suggest ? well for the time being how would I do it using MySQL_? I will change the rest to prevent injection attacks - thanks – Matt Aug 26 '15 at 15:26
  • Once you get the query correct you can use it with *any* MySQL API. I am just suggesting that you quit using the older API as it is being done away with. – Jay Blanchard Aug 26 '15 at 15:29
  • thanks - I will. Do you have any idea why the code does not get every 25th row ? – Matt Aug 26 '15 at 15:47

1 Answers1

0

Fixed it the problem was that I was using color = 'green' or color = 'red' should have used color REGEXP 'green' or color REGEXP 'red' works fine now with this.

Matt
  • 7
  • 4