Jump to content

Recommended Posts

Ok I have a join issue I can not solve.  First I will give the two table stucture

 

 

 

members                                                      categorys

memberID                                                     

fav_1                                                                            catID

fav_2                                                                            category

fav_3

fav_4

fav_5

 

 

Now when the member select their top five favourites it saves the catID an not the name.  When I come to echo the members favourite obviously it show the number.  I've been trying to write an inner join I believe to make it echo the name instead but I keeps failing.  can somebody please tell me where i have gone wrong in the code? 

 

<?php 
						  
						$query = mysql_query("SELECT club_category.*, members.* FROM club_category INNER JOIN members ON club_category.catID = members.fav_1 WHERE memberID= ".$User['memberID']." ");
$result = mysql_fetch_array($query); 
						  
						  if ($User['fav_1']!="") { if($result['fav_1']) {echo " <li>{$result['categorys']}</li> ";}}
						  		if ($User['fav_2']!="") {if ($User['fav_2']){echo " <li>{$result['categorys']}</li> ";}}

						    ?>

 

It just seems to be showing the result of fav_1 two times on the screen.

Link to comment
https://forums.phpfreaks.com/topic/234071-join-issues/
Share on other sites

Hi

 

You effectively have duplicate data in different columns. You would be best pulling off the favourites into another table with one row per favourite per person. Would also make it FAR easier should you chose to increase or reduce the number of favourites in the future.

 

However if you must have the columns:-

 

<?php

$query = mysql_query("SELECT members.memberID, club_category1.category AS Cat1, club_category2.category AS Cat2, club_category3.category AS Cat3, club_category4.category AS Cat4, club_category5.category AS Cat5
FROM members
LEFT OUTER JOIN club_category club_category1 ON members.fav_1 = club_category1.catID
LEFT OUTER JOIN club_category club_category2 ON members.fav_2 = club_category2.catID
LEFT OUTER JOIN club_category club_category3 ON members.fav_3 = club_category3.catID
LEFT OUTER JOIN club_category club_category4 ON members.fav_4 = club_category4.catID
LEFT OUTER JOIN club_category club_category5 ON members.fav_5 = club_category5.catID
WHERE memberID= ".$User['memberID']." ");


while($row = mysql_fetch_array($query))
{
if ($row['Cat1']) echo " <li>{$row['Cat1']}</li> ";
if ($row['Cat2']) echo " <li>{$row['Cat2']}</li> ";
if ($row['Cat3']) echo " <li>{$row['Cat3']}</li> ";
if ($row['Cat4']) echo " <li>{$row['Cat4']}</li> ";
if ($row['Cat5']) echo " <li>{$row['Cat5']}</li> ";
}

?>

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/234071-join-issues/#findComment-1203093
Share on other sites

Ok I made the changes to my database as suggested.  It now has favourites table with the memberID, the "favourite" number (which is the same as the catID in the category table) and the order in which they chose that favourite......

 

 

FAVOURITES                                CATEGORY                                                       

FavID,                                            catID

memberID,                                      category

favourite,

order

 

I created a new join but again it does not work :(

 

$new_fav = mysql_query("SELECT favourites.*, category.* FROM favourites INNER JOIN ON favourites.favourite = category.catID WHERE favourite.memberID = ".$User['memberID']." ");
while($row1 = mysql_fetch_array($new_fav))
{
if ($row1['favourite']) echo " <li>{$row1['favourite']}</li> ";

}

Link to comment
https://forums.phpfreaks.com/topic/234071-join-issues/#findComment-1203438
Share on other sites

sorry that was a typo.  It is there it says.....

 

	$new_fav = mysql_query("SELECT favourites.*, category.* FROM favourites INNER JOIN category ON favourites.favourite = category.catID WHERE favourite.memberID = ".$User['memberID']." ");
	while($row1 = mysql_fetch_array($new_fav))
	{
		if ($row1['favourite']) echo " <li>{$row1['favourite']}</li> ";

	}

 

I dont recieve any error.  Just nothing appears.

Link to comment
https://forums.phpfreaks.com/topic/234071-join-issues/#findComment-1203446
Share on other sites

Sounds like you have errors turned off. Try this to get the error message:

error_reporting(E_ALL);
ini_set('display_errors','On');
$new_fav = mysql_query("SELECT favourites.*, category.* FROM favourites INNER JOIN category ON favourites.favourite = category.catID WHERE favourite.memberID = ".$User['memberID']." ") or die(mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/234071-join-issues/#findComment-1203454
Share on other sites

Brill ok I have my error......

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/sites/edit_member.php on line 239

 

It seems to be referring to...

 

while($row1 = mysql_fetch_array($new_fav))

 

 

from

 

    $new_fav = mysql_query("SELECT favourites.*, club_category.* FROM favourites INNER JOIN club_category ON favourites.favourite = club_category.catID WHERE favourite.memberID = ".$User['memberID']." " or die(mysql_error())) ;
	while($row1 = mysql_fetch_array($new_fav))
	{
		if ($row1['favourite']) echo " <li>{$row1['favourite']}</li> ";

	}

 

It could also be referencing to the line above though

Link to comment
https://forums.phpfreaks.com/topic/234071-join-issues/#findComment-1203458
Share on other sites

the table structure again......

 

FAVOURITES                                CATEGORY                                                       

FavID,                                            catID

memberID,                                      category

favourite,

order

 

catID and favourite are the same.  1 User will have 5 entries in FAVOURITES and they will all be different.

Link to comment
https://forums.phpfreaks.com/topic/234071-join-issues/#findComment-1203460
Share on other sites

Your parenthesis are wrong, which is why $new_fav isn't a valid resource. Change

$new_fav = mysql_query("SELECT favourites.*, club_category.* FROM favourites INNER JOIN club_category ON favourites.favourite = club_category.catID WHERE favourite.memberID = ".$User['memberID']." " or die(mysql_error())) ;

to

$new_fav = mysql_query("SELECT favourites.*, club_category.* FROM favourites INNER JOIN club_category ON favourites.favourite = club_category.catID WHERE favourite.memberID = ".$User['memberID']." ") or die(mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/234071-join-issues/#findComment-1203477
Share on other sites

ok if I echo the query as asked I get this........

 

 

SELECT favourites.*, club_category.* FROM favourites INNER JOIN club_category ON favourites.favourite = club_category.catID WHERE favourite.memberID = 0000000003

 

I dont get whats going wrong.  The query works great if I write it.....

$fav = mysql_query("SELECT * FROM favourites WHERE memberID = ".$User['memberID']." ORDER BY `order`");
										while($row = mysql_fetch_array($fav))
{
if ($row['favourite']) echo " <li>{$row['favourite']}</li> ";

}

 

This issue is that it only echos the numbers out this way.  Ive done many JOIN's now and they have never been a problem.  I just cant see whats wrong at all.

Link to comment
https://forums.phpfreaks.com/topic/234071-join-issues/#findComment-1203502
Share on other sites

Try pasting the echoed query out into your favorite MySQL editor and see if it gives you any errors, or the results you expected. It might be a logical error. I'd make any needed tweaks in the MySQL editor until you get what you want then move it into your PHP file.

Link to comment
https://forums.phpfreaks.com/topic/234071-join-issues/#findComment-1203505
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.