Jump to content


Photo

Looping Query


  • Please log in to reply
6 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 18 September 2006 - 03:59 PM

Hi all

I have a php query which checks a database for a particular number, for example

23

if the query does no find a result, I have a 2nd query which checks for the next number, for example

24

I 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 for

23

then loop to do

24

then loop to do

25

and so on.

The query I use is

<?
$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)) {
?>

Is this an easy function to add?

Any help would be great.

Thanks in advance

Dave

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 18 September 2006 - 04:06 PM

<?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

?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 18 September 2006 - 04:13 PM

slightly different approach:
<?php
$sql = -1;
// set $i to your minimum and $i <= MAXIMUM
for ($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
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 18 September 2006 - 04:20 PM

if you know your min and max, just do this, without a loop:

<?php
   $min = 1; // or whatever
   $max = 10; // or whatever
   $sql = "select * from data where number between '$min' and '$max'";
   $result = mysql_query($sql); 
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 18 September 2006 - 04:36 PM

if you know your min and max, just do this, without a loop:


actually, based on his first post, he only wants to continue the loop if Nothing is returned from the existing query... hence the loops ;)
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 18 September 2006 - 04:46 PM

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.

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 18 September 2006 - 05:39 PM

SELECT * FROM data WHERE number >= $min LIMIT 1
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users