开发者

the query is breaking. How to skip rows PHP

开发者 https://www.devze.com 2023-01-04 01:05 出处:网络
I have a question probably lame but it made me stuck I have the a db query $query_Recordset10 = \"SELECT * FROM products

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 = mys开发者_开发知识库ql_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.


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


PROBLEM SOLVED. I still have a lot to learn.

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

this is the wright line but my mistake was how $ID is generated.

Thank you all.


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.


Use foreach to loop over an array rather than trying to access by indexes (if you're fetching every value):

foreach ($records as $record) {
}

instead of

for ($i = 0; $i < count($records); $i++) {
}

or

$i = 0;
while ($i < count($records)) {

    $i++;
}

or

$i = 0;
do {

} while (++$i < count($records));


Use this query instead:

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

This will give you the next element after the one with ID of $ID.

0

精彩评论

暂无评论...
验证码 换一张
取 消