Jump to content

Recommended Posts

I am not too experienced with MySQL and I've read a few tutorials on JOIN and UNION but can't get my head around it to be honest - can someone help me out please? I'm fairly sure I need to combine these queries to get my desired result...

 

I have 2 tables, one called promo and another called items. What I want is...

 

This is a Promotional [title]

I am the promotional description for this promo! [description]

Items in this promotional are:

- Item 1

- Item 2

- Item 3

 

Code so far:

 



$result  = mysql_query("SELECT * FROM promo WHERE id = '$promo_id'") or die('Error : ' . mysql_error());
while($row = mysql_fetch_array($result))
{
echo "<p>".$row['title']."</p>";
echo "<p>".$row['description']."</p>";
}

$result  = mysql_query("SELECT * FROM item WHERE promo_id = '$promo_id'") or die('Error : ' . mysql_error());
while($row = mysql_fetch_array($result))
{
echo "<p>".$row['filename']."</p>";
}

 

Using MySQL 5. Thanks for your help  :D

Link to comment
https://forums.phpfreaks.com/topic/210158-multiple-queries/
Share on other sites

I think the mySQL you are looking for is something like...

 

SELECT * FROM promo JOIN item ON promo.id = item.promo_id WHERE promo.id='$promo_id'

 

The important part is ON promo.id = item.promo_id. This tells mysql to take your item table and merge the items together with their promotions, but only where the promo_id field matches an id of a promotion on the promo table.

Link to comment
https://forums.phpfreaks.com/topic/210158-multiple-queries/#findComment-1096752
Share on other sites

Thanks for that. I have made some progress but I am still struggling slightly. Believe me I've worked on this for a few hours now and can't crack it!!! Maybe I'm just too simple!

 

My php now looks like so:

 

		
$result  = mysql_query("SELECT * FROM promo JOIN items ON promo.id = items.promo_id WHERE promo.id='$promo_id'") or die('Error : ' . mysql_error()); 
	while($row = mysql_fetch_array($result))
	{
	echo "<p>".$row['promo.title']."</p>";
	echo "<p>".$row['description']."</p>";
	echo "<p>".$row['filename']."</p>";
	}

 

The outcome is:

 

Description test 1

 

filename.jpg

 

Description test 1

 

filename_2.jpg

 

I don't seem to be able to access the Promo Title at all ?

 

Also it's repeating and I can sort of see why from the PHP - what do you recommend to just get my desired outcome (OP).

Link to comment
https://forums.phpfreaks.com/topic/210158-multiple-queries/#findComment-1097191
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.