lszanto Posted September 27, 2007 Share Posted September 27, 2007 I'm working on a simple script at the moment and it requires to sort different data into sections and these sections are chosen by there 'list' title, for some reason the sql only loops once so I need to know why its not looping more, if you need more info please ask and thanks in advance. <?php //Make query. $sql = "SELECT * FROM lists"; //Make query into result. $result = mysql_query($sql); //Run query loop. while($row = mysql_fetch_array($result)) { //Get list. $list = $row['title']; //Show list. echo "<br /><h3>$list</h3>"; //Make new query. $sql = "SELECT title, description, price FROM wish_list WHERE list='$list'"; //Make result. $result = mysql_query($sql); //Run result. while($data = mysql_fetch_array($result)) { //Show each piece of data. echo "<strong>Title:</strong>" . $data['title'] . "<br />"; echo "<strong>Description:</strong>" . $data['description'] . "<br />"; echo "<strong>Price:</strong> $" . $data['price'] . "<br /><br />"; } //Break lines. echo "<br /><br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70877-solved-mysql-loop-unknown-error/ Share on other sites More sharing options...
marcus Posted September 27, 2007 Share Posted September 27, 2007 Yours looks fine, but give this a go. <?php $sql = "SELECT * FROM `lists"; $res = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ $list = $row['title']; echo "<br /><h3>$list</h3>\n"; $sql2 = "SELECT * FROM `wish_list` WHERE `list`='$list'"; $res2 = mysql_query($sql2) or die(mysql_error()); while($row2 = mysql_fetch_assoc($res2)){ echo "<strong>Title:</strong> ". $row2['title'] ." <br />\n"; echo "<strong>Description:</strong> ". $row2['description'] ."<br />\n"; echo "<strong>Price:</strong> ". $row['price'] ."<br />\n"; } echo "<br /><br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70877-solved-mysql-loop-unknown-error/#findComment-356289 Share on other sites More sharing options...
hemlata Posted September 27, 2007 Share Posted September 27, 2007 Hello, That problem is because of the same variable names that you are using for both the while loops, queries. Modify the variable names and see if you get the issue solved. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/70877-solved-mysql-loop-unknown-error/#findComment-356298 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.