CanMan2004 Posted September 18, 2006 Share Posted September 18, 2006 Hi allI have a php query which checks a database for a particular number, for example23if the query does no find a result, I have a 2nd query which checks for the next number, for example24I want to loop the query, so that it keeps returning results with the number above the one just queried.So it would do the query for23then loop to do24then loop to do25and so on.The query I use is[code]<?$sql = "SELECT * FROM data WHERE `number` LIKE '%".$number."%'";$show = @mysql_query($sql,$connection) or die(mysql_error());$num = mysql_num_rows($show);while ($rows = mysql_fetch_array($show)) {?>[/code]Is this an easy function to add?Any help would be great.Thanks in advanceDave Link to comment https://forums.phpfreaks.com/topic/21161-looping-query/ Share on other sites More sharing options...
Orio Posted September 18, 2006 Share Posted September 18, 2006 [code]<?php//$number is the number used in the query$rows=0;while($rows==0){ $sql = "SELECT * FROM data WHERE `number` = '$number'"; $result = mysql_query($sql); $rows = mysql_num_rows($result); if($rows==0) { $number++; }}echo "Result found for $number";while ($row = mysql_fetch_array($show)) {//rest of code?>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/21161-looping-query/#findComment-94035 Share on other sites More sharing options...
obsidian Posted September 18, 2006 Share Posted September 18, 2006 slightly different approach:[code]<?php$sql = -1;// set $i to your minimum and $i <= MAXIMUMfor ($i = 23; $i <= 26; $i++) { $sql = mysql_query("SELECT * FROM data WHERE `number` = '$id'"); if (mysql_num_rows($sql) > 0) break;}// use $sql to display your results?>[/code] Link to comment https://forums.phpfreaks.com/topic/21161-looping-query/#findComment-94037 Share on other sites More sharing options...
.josh Posted September 18, 2006 Share Posted September 18, 2006 if you know your min and max, just do this, without a loop:[code]<?php $min = 1; // or whatever $max = 10; // or whatever $sql = "select * from data where number between '$min' and '$max'"; $result = mysql_query($sql); ?>[/code] Link to comment https://forums.phpfreaks.com/topic/21161-looping-query/#findComment-94041 Share on other sites More sharing options...
obsidian Posted September 18, 2006 Share Posted September 18, 2006 [quote author=Crayon Violent link=topic=108508.msg436601#msg436601 date=1158596452]if you know your min and max, just do this, without a loop:[/quote]actually, based on his first post, he only wants to continue the loop if [b]Nothing[/b] is returned from the existing query... hence the loops ;) Link to comment https://forums.phpfreaks.com/topic/21161-looping-query/#findComment-94049 Share on other sites More sharing options...
.josh Posted September 18, 2006 Share Posted September 18, 2006 hmmm....yeah i suppose i see what you're saying. if he wants the data from row one, but nothing is found, then he wants the data from row 2, but if nothing is found, then get the value from row 3, etc.. okay i'll buy that. Link to comment https://forums.phpfreaks.com/topic/21161-looping-query/#findComment-94057 Share on other sites More sharing options...
Barand Posted September 18, 2006 Share Posted September 18, 2006 SELECT * FROM data WHERE number >= $min LIMIT 1 Link to comment https://forums.phpfreaks.com/topic/21161-looping-query/#findComment-94083 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.