Jump to content

I need to pull data from two tables


jonw118

Recommended Posts

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
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.