Jump to content

Recommended Posts

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

[code]<?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 />';
}

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/21739-sql-loop-not-looping/
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/21739-sql-loop-not-looping/#findComment-97075
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.