Jump to content


Photo

sql loop not looping


  • Please log in to reply
6 replies to this topic

#1 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 23 September 2006 - 12:48 AM

my code (see below) should work as follows
1) run a sql query and get get all the topics and their ID# (this returns 4 topics right now)

2) while the topics are being returned 1 row at a time I take the topic ID and run a second SQL query using it to find all the links in the link table and echo them out

3) once all the links are done, we should move on to the next topic and then so on doing the same thing for each of the 4 topics

the problem is that this code is only working for the very first topic.... WHY!?!?!

<?php
include $_SERVER['DOCUMENT_ROOT'].'/sbqa/layout2.php';
$row='';
	  $sql = mysql_query("SELECT * FROM link_types");
			while($row = mysql_fetch_array($sql)){
			stripslashes(extract($row));
				echo '<table border="0" align="center">
						  <tr>
							<td colspan="2" class="box_title">'.$type.'</td>
						  </tr>';
					$sql = mysql_query("SELECT * FROM 
						links WHERE (type_1='$type_id' OR type_2='$type_id' OR type_3='$type_id') ORDER BY title");
						while($row = mysql_fetch_array($sql)){
						stripslashes(extract($row));
						 echo'<tr><td><a href="'.$url.'">'.$title.'</a></td><td>'.$description.'</td>
							  </tr>';
							  $url='';
							  $title='';
							  $description='';
						}
				echo '</table> <br /> <br />';
				}

?>


#2 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 23 September 2006 - 01:16 AM

Can you tell me what it is doing?  Perhaps viewing the source and telling me what is says?
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#3 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 23 September 2006 - 01:19 AM

it only runs one topic and fills in all the subcategories of that topic but never moves back into the first while loop to finish it out

#4 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 23 September 2006 - 01:26 AM

looks like you're resetting your $sql variable in the middle of the while($row = mysql_fetch_array($sql) loop. Try a different variable name for the second query.

#5 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 23 September 2006 - 01:28 AM

Good catch Michael.  He's write, Mark.
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#6 .josh

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

Posted 23 September 2006 - 01:29 AM

you are overwriting the $row from the first while loop, with the 2nd while loop

edit: ^^ the $sql overwrite is okay.  the main one is only needed one time, in order to get the results. the result source is then used in the outer while loop.  So it doesn't matter that it is over-written inside that loop.  It's the $row he is over-writing.  the outer loop is based on $row, but he starts his inner loop using the same variable name. the inner loop loops through the over-written $row and then breaks back to the outer loop, which then becomes false, because it checks $row, which the inner loop already incremented the pointer to the end of it. 

you need to use a different variable name for your inner loop, instead of $row.

editedit: okay actually, yes, you need to use a diff var besides $sql, as well. i just noticed that you did not seperate the query string from the query function. so yes, you need to use some different var than $sql, but you also need to do the same for $row
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 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 23 September 2006 - 09:00 PM

yeah he is... i dont kn wo why but ii swear ive used the same code previously wihtout any issue grrr thank you so much it was annoying me i couldnt get it....




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users