jonw118 Posted November 23, 2009 Share Posted November 23, 2009 So - I have data I need to pull from two tables in one MySQL database. When you create an Image Gallery you give it a name - which is stored in the table "gallery" under the column "gallery_name". However, the gallery page doesn't display the Gallery Name which I want it to. Currently the page has at the top: <?php include_once('/home/***/admin/info.php'); $gallery_id=$_GET['id']; $query=mysql_query("SELECT * FROM gallery_image where gallery_id=$gallery_id order by priority") ; $res = mysql_num_rows($query); ?> So, I want to pull the field "gallery_name" from the the table "gallery". I'd imagine I'd insert something along the lines of: <?php echo $res['gallery_name']; ?> in the page where I want the code insterted. But just not sure how to get it to pull that from the table "gallery" since right now it's just pulling from the table "gallery_name". Thanks! Any thoughts or help would be GREATLY appreciated. Quote Link to comment Share on other sites More sharing options...
kickstart Posted November 23, 2009 Share Posted November 23, 2009 Hi How are the gallery and gallery image tables linked? Does the gallery image contain a gallery id that refers to the row on the gallery table? All the best Keith Quote Link to comment Share on other sites More sharing options...
jonw118 Posted November 23, 2009 Author Share Posted November 23, 2009 Keith - thanks for the response! They are linked by ID. The table "gallery" has the columns: -id -gallery_name -main_desc -priority The table "gallery_image" has: -id -gallery_id -title -description -priority Hope that can give some more insight! Thanks again for the help! Quote Link to comment Share on other sites More sharing options...
kickstart Posted November 23, 2009 Share Posted November 23, 2009 Hi This will get you the gallery details for each image returned <?php include_once('/home/***/admin/info.php'); $gallery_id=$_GET['id']; $query=mysql_query("SELECT * FROM gallery a JOIN gallery_image b ON a.id = b.gallery_id where gallery_id=$gallery_id order by priority") ; $res = mysql_num_rows($query); ?> Note it is generally not best practice to use SELECT *, instead of using * specify the actual columns you want. All the best Keith Quote Link to comment Share on other sites More sharing options...
jonw118 Posted November 23, 2009 Author Share Posted November 23, 2009 Keith- thanks again for the assistance. I'm getting an error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/***/main/gallery.php on line 5 Line 5 is: $res = mysql_num_rows($query); Quote Link to comment Share on other sites More sharing options...
jonw118 Posted November 23, 2009 Author Share Posted November 23, 2009 I just did a degub and this was the result "Query failed: Query was empty". Quote Link to comment Share on other sites More sharing options...
kickstart Posted November 23, 2009 Share Posted November 23, 2009 Hi Unless I have an error in the SQL (nothing jumps out, try it in phpmyadmin or equivalent) then that suggests there is no matching gallery record for a gallery image record. All the best Keit Quote Link to comment Share on other sites More sharing options...
jonw118 Posted November 23, 2009 Author Share Posted November 23, 2009 I think I see what the problem is here - not sure how I got that empty error - can't duplicate it. But I just the mysql_error - and got the result: "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/***/main/gallery.php on line 5 select: Column 'priority' in order clause is ambiguous" I think this is because both gallery has a priority and gallery_image has a priority. For the gallery priority, it is irrelevant on this page (as it set priority for the gallery options page). But it does need the priority for the gallery_image as it sets the sort order for the pictures. Quote Link to comment Share on other sites More sharing options...
kickstart Posted November 23, 2009 Share Posted November 23, 2009 Hi Ah, right. My fault for not spotting that. SELECT * FROM gallery a JOIN gallery_image b ON a.id = b.gallery_id where gallery_id=$gallery_id order by b.priority All the best Keith Quote Link to comment Share on other sites More sharing options...
jonw118 Posted November 23, 2009 Author Share Posted November 23, 2009 Thanks so much for your help Keith - I really, really appreciate it! Quote Link to comment Share on other sites More sharing options...
jonw118 Posted November 23, 2009 Author Share Posted November 23, 2009 Keith- soooo sorry - hope you don't mind me asking you one more thing. On one of the gallery pages I have 3 photos displayed - and the gallery_name (which we were working on adding) at the top of the page is showing 3 different times. I'm using <?php echo $res['gallery_name']; ?> to show the name. Again, thank you so much for your help. Hope this is something simple I'm missing. I've been pulling the hair trying different things. Quote Link to comment Share on other sites More sharing options...
kickstart Posted November 24, 2009 Share Posted November 24, 2009 Hi That really comes down to php coding. Save the previous gallery name and only output the gallery name when it is different to the previous one. All the best Keith 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.