Jump to content

How do code a query over 3 tables?


rocky48

Recommended Posts

I am trying to write a query in PHP on a MYSQL database to retrieve data from more than one table.

 

My database is a database of greetings card verses and is made up of 3 tables.

VERSES

-ID=int>>PK

-Event=int

-Sub_Type=int

-Verse=varchar

 

EVENTS

-ID=int..PK

-Event_Type=varchar

 

EVENT_SUB

-ID=int>>PK

-Event_Sub_Type=varchar

 

A previous script uses $POST to identify which Event_Type is chosen and from the array it will show the ID number for the EVENT_SUB and therefore it will also mean that I can use the Event_Sub_Type in the output to the screen.

In have looked at JOINS, but can not see how you write a JOIN which uses 3 tables.

Once I get the logic for the SELECT QUERY right I should be able to change the PHP code to suit.

Can anyone help?

Link to comment
Share on other sites

Thanks Jesi that was very helpful, but I still have a problem!

 

I have managed to get most of the information onto the table, but I need to display the ID in the verses table and for some reason I get the ID from the Event_Sub table, even if I qualify the table name (Verses.ID).  Can anyone spot where I am going wrong?

 

Here is the code:

$get_Event_sql  = "SELECT Verses.ID, Verses.Verse, Verses.Sub_Type, Verses.Event, Events.ID, Events.Event_Type, Event_Sub.ID, Event_Sub.Event_Sub_Type
FROM Verses
LEFT JOIN Events
ON Verses.Event = Events.ID
LEFT JOIN Event_Sub
ON Verses.Sub_Type = Event_Sub.ID
WHERE Verses.Event = '".$_POST["Event_Type"]."' 
ORDER BY Verses.ID ASC";


$get_Event_res = mysqli_query($mysqli, $get_Event_sql) or die(mysqli_error($mysqli));

//create the display string
$display_block = "
<p> The Event Type is <b> ['Event_Sub.Event_Type']</b> </p>
<table width=\"50%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\" BGCOLOR=\"#87CEEB\" >
<tr>
<th>ID</th>
<th>VERSE</th>
<th>MOOD</th>
</tr>";

while ($Verse_info = mysqli_fetch_array($get_Event_res)) {
	$Event_id = $Verse_info['Verses.ID'];
	$Verse_text = nl2br(stripslashes($Verse_info['Verse']));
	$Mood_info = $Verse_info['Event_Sub_Type'];

	//add to display
 	$display_block .= "
	<tr>
	<td width=\"1%\" valign=\"top\">".$Event_id."<br/></td>
	<td width=\"35%\" valign=\"top\">".$Verse_text."<br/></td>
	<td width=\"35%\" valign=\"top\">" .$Mood_info."<br/></td>
	</tr>";
}

//free results
mysqli_free_result($get_Event_res);
mysqli_free_result($verify_Event_res);

//close connection to MySQL
mysqli_close($mysqli);

//close up the table
$display_block .= "</table>";
}

 

I hope someone can help as I have exhausted my knowledge.

Link to comment
Share on other sites

In the select statement you'll use Verses.ID but in the array returned it will just be ID. The problem is that you have two three columns named ID. Select each using an AS to rename them so they are different.

$get_Event_sql  = "SELECT Verses.ID AS versesID, Verses.Verse, Verses.Sub_Type, Verses.Event, Events.ID AS eventsID, Events.Event_Type, Event_Sub.ID AS event_subID, Event_Sub.Event_Sub_Type
FROM Verses
LEFT JOIN Events
ON Verses.Event = Events.ID
LEFT JOIN Event_Sub
ON Verses.Sub_Type = Event_Sub.ID
WHERE Verses.Event = '".$_POST["Event_Type"]."' 
ORDER BY Verses.ID ASC";

 

Then it will be $row['versesID'] for the verses table ID.

 

 

Link to comment
Share on other sites

Thanks again Jesi!

As my knowledge is limited or I'm just plain thick today, can you show me the syntax for the $row['versesID'].

 

The code below gives an error:

Fatal error: Function name must be a string in C:\websites\Test\MshowType.php on line 50

 

while ($Verse_info = mysqli_fetch_array($get_Event_res)) {
	$Event_id = $Verse_info($row['VersesID']);
	$Verse_text = nl2br(stripslashes($Verse_info['Verse']));
	$Mood_info = $Verse_info['Event_Sub_Type'];

 

If not perhaps I misunderstood your last sentance.

 

Thanks again!

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.