atticus Posted November 13, 2007 Share Posted November 13, 2007 I want to query the database and return the username once for each record while returning all the files associated with the cust_id. I am using two while loops, but it is not working. Any suggestions: $sql = "SELECT * FROM upload2 ORDER BY cust_id"; $query = mysql_query($sql); while($myrow = mysql_fetch_array($query)) { echo "".$myrow['cust_id']."<br />"; while($row = mysql_fetch_array($query)) { echo "<a href=\"view.php?id=$row[id]\">".$row['title']."</a><br />"; echo "<br />"; } } I am not getting any errors, its only displaying the last record in the database. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 14, 2007 Share Posted November 14, 2007 There are two ways to do this. One is to have one loop using mysql_fetch_array(), and to detect when cust_id changes. When it changes, you print out the new one and continue printing files. The other way is to have one query that fetches cust_id only, and an inner loop with a second query fetching that user's files. The first way should be faster, the second more memory efficient (if memory is an issue). In either case, you can't do a second loop inside another loop using the same query result. That is what is causing the problem. Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 14, 2007 Share Posted November 14, 2007 in addition.... you have two loop but the first loop will only run once and the next loop finish or extract all the data in your query while($myrow = mysql_fetch_array($query)) <-- this will only loop once while($row = mysql_fetch_array($query)) <-- loop till it gets all the contents in your query Quote Link to comment 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.