Jump to content

Joining 3 tables question.


therealwesfoster

Recommended Posts

I have 3 tables.

-----

events

id_event

title

thumb

date

id_venue

phone

-----

venues

id_venue

name

address

phone

-----

gallery

id_gallery

title

id_event

url

thumb

-----

 

I'm just wanting to grab all of their data where their id_event = 15. And I want to be able to put them into an array and call them by name.

 

Here is what I've got.

$sql = mysql_query ("SELECT e.*, v.*, g.* FROM events e JOIN venues v ON e.id_venue = v.id_venue LEFT JOIN gallery g ON g.id_event = e.id_event ORDER BY e.id_event DESC LIMIT 1") or die(mysql_error());
$row = mysql_fetch_array($sql);

 

Then I want to echo it like so:

echo $row['g.title']."<br />";
echo $row['e.title'];

 

But nothing shows up unless I just echo $row['title']. But the problem with that is, I want to specify which table I'm echoing the data from (since gallery and events both have a title column)

 

How?

Link to comment
https://forums.phpfreaks.com/topic/88945-joining-3-tables-question/
Share on other sites

You would have to call them as:

SELECT e.title as event_title,g.title as gallery_title FROM events e JOIN venues v ON e.id_venue = v.id_venue LEFT JOIN gallery g ON g.id_event = e.id_event ORDER BY e.id_event DESC LIMIT 1

 

You should try to get in the habit of having unique column name like so:

 

events

event_id

event_title

event_thumb

event_date

venue_id

event_phone

-----

venues

venue_id

venue_name

venue_address

venue_phone

-----

gallery

gallery_id

gallery_title

event_id

gallery_url

gallery_thumb

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.