Search code examples
phpcyclerowsbreakskip

the query is breaking. How to skip rows PHP


I have a question probably lame but it made me stuck I have the a db query

 $query_Recordset10 = "SELECT * FROM products 
  WHERE razdel='mix' AND ID='$ID+1' AND litraj='$litri' ORDER BY ID ASC";
$Recordset10 = mysql_query($query_Recordset10, $victor) or die(mysql_error());
$row_Recordset10 = mysql_fetch_array($Recordset10);
$totalRows_Recordset10 = mysql_num_rows($Recordset10);

This is the query for the next product in the line based in the ID of the current product thats on the page.

But if the next product matching the criteria in the query is 2 or more ID's ahead my cycle breaks. So is there a way for skipping this rows and get the next ID matching the criteria.

Thank you very much.


Solution

  • Change your query to this:

    $query_Recordset10 = "SELECT * FROM products 
        WHERE razdel='mix' AND ID > '$ID' AND litraj='$litri' ORDER BY ID ASC LIMIT 1";
    

    So you still only get 1 row returned (if there's anything to return), but you'll be returning the next row (according to ORDER BY ID ASC) versus (potentially) the row with an incremental ID.