NLT Posted May 1, 2012 Share Posted May 1, 2012 while($comp = mysql_fetch_assoc($company)) { echo "company name: $comp['name'] <br>"; $c = mysql_fetch_assoc(mysql_query("SELECT * FROM comp_images WHERE company='". $comp['name'] ."'")); echo $c; } When I use that, it only echos one image URL, none others. Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/ Share on other sites More sharing options...
Jessica Posted May 1, 2012 Share Posted May 1, 2012 You should be doing a join, not queries in a loop. Example: SELECT comp_images.*, company.* FROM comp_images LEFT JOIN company ON company.company_id = comp_images.company_id Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342024 Share on other sites More sharing options...
NLT Posted May 1, 2012 Author Share Posted May 1, 2012 But how would I echo them out on a different line etc? Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342026 Share on other sites More sharing options...
NLT Posted May 1, 2012 Author Share Posted May 1, 2012 Nevermind - I understand it now. But it is still only displaying one. Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342027 Share on other sites More sharing options...
xyph Posted May 1, 2012 Share Posted May 1, 2012 Post your code Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342031 Share on other sites More sharing options...
NLT Posted May 1, 2012 Author Share Posted May 1, 2012 $company = mysql_query("SELECT comp_images.*, company.* FROM comp_images LEFT JOIN company ON num > 2"); while($comp = mysql_fetch_assoc($company)) { echo "company name: $comp['name'] <br>"; $c = mysql_fetch_assoc(mysql_query("SELECT * FROM comp_images WHERE company='". $comp['name'] ."'")); echo $c; } Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342035 Share on other sites More sharing options...
Jessica Posted May 1, 2012 Share Posted May 1, 2012 Don't do the query inside the loop! Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342044 Share on other sites More sharing options...
NLT Posted May 1, 2012 Author Share Posted May 1, 2012 Don't do the query inside the loop! Okay, I've done that. Now it does display that.. but I'm not sure how I can split them up. It's now saying: image,companyname image,companyname (or vice versa) But I want it to be like: companyname imageimageimageimage Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342051 Share on other sites More sharing options...
Jessica Posted May 1, 2012 Share Posted May 1, 2012 You can either do two queries (not in a loop, but two separate single queries) to get the list of companies and the list of all images sorted by country, OR you can do this (which is how I'd do it) In the join the ON needs to represent the relationship between company and comp_images. There should be a unique ID for each company. <?php $sql = mysql_query("SELECT comp_images.*, company.* FROM comp_images LEFT JOIN company ON company.id = comp_images.company_id ORDER BY company.name"); $last_company_id = 0; while($comp = mysql_fetch_assoc($sql)) { if($comp['id'] != $last_company_id){ echo "company name: $comp['name'] <br>"; $last_company_id = $comp['id']; } echo $comp['image_info_here'].'<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342054 Share on other sites More sharing options...
Psycho Posted May 1, 2012 Share Posted May 1, 2012 EDIT: jesirose beat me to it, but I'll post this nonetheless since I don't like seeing queries written inside the mysql_query() function as it makes debugging more difficult You didn't show your current code, so this is only a guess based upon the table fields $query = "SELECT comp_images.image, company.name FROM company LEFT JOIN comp_images ON company.id = comp_images.company"; $result = mysql_query($query) or die(mysql_error()); $company = false; while($row = mysql_fetch_assoc($result)) { if($company != $row['name']) { $company = $row['name']; echo "<br><b>{$company}</b><br>\n"; } echo $row['image'}; } Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342058 Share on other sites More sharing options...
NLT Posted May 1, 2012 Author Share Posted May 1, 2012 How would I go about two queries? If I use SELECT on them, how would I make it loop without in a while? Or would I make it into a while loop? I'm not sure how I would make it into a while loop though, because of the mysql_fetch_assoc for the while condition. EDIT: Explanation why: I'm not really a big fan of the LEFT JOIN and things, I'd prefer use SELECT statements if you know what I mean. It may be because I'm not the biggest under-stander of them.. Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342062 Share on other sites More sharing options...
xyph Posted May 1, 2012 Share Posted May 1, 2012 A LEFT JOIN is still a SELECT. If you plan on using a relational database, I'd suggest starting to get comfortable with JOINs. They're very powerful, and efficient. Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342066 Share on other sites More sharing options...
NLT Posted May 1, 2012 Author Share Posted May 1, 2012 A LEFT JOIN is still a SELECT. If you plan on using a relational database, I'd suggest starting to get comfortable with JOINs. They're very powerful, and efficient. Then the code you gave me still does the same. It displays x and then y straight after, and repeats it until it's done. Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342073 Share on other sites More sharing options...
xyph Posted May 1, 2012 Share Posted May 1, 2012 I didn't give you any code. Everything posted above will behave the way you want it to, but it might not work by just copying it in to your code. What is x? What is y? Post some code, please. You've probably implemented it incorrectly. Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342083 Share on other sites More sharing options...
Jessica Posted May 1, 2012 Share Posted May 1, 2012 I don't like seeing queries written inside the mysql_query() function as it makes debugging more difficult I very much agree, great point. Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342088 Share on other sites More sharing options...
NLT Posted May 3, 2012 Author Share Posted May 3, 2012 Got it working, thanks all. Quote Link to comment https://forums.phpfreaks.com/topic/261910-query-nested-in-a-while/#findComment-1342752 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.