954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help!howto put the where clauses in left join

i have a gallery SCript which i saw in tutorial sitepoint.com
everything is working well except that i want to limit users,which means the albums will be opening accoding to user=name
WHERE SHOULD I PUT THE WHERE CLAUSE LIKE THIS user='$user'
in the script below.

query( "SELECT c.category_id,c.category_name,photo_filename,COUNT(photo_id)
						FROM gallery_category  as c 
						LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id 
						GROUP BY c.category_id" );


here is the link of the tutorial,any help will be appreciated. http://www.sitepoint.com/article/php-gallery-system-minutes/

mrcniceguy
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 17
Solved Threads: 8
 

According to the mysql manual which has no mention of left join, it would be one of the following:

mysql_query( "SELECT c.category_id,c.category_name,photo_filename,COUNT(photo_id)
FROM gallery_category  as c 
WHERE user='$user'
LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id 
GROUP BY c.category_id" );

or

mysql_query( "SELECT c.category_id,c.category_name,photo_filename,COUNT(photo_id)
FROM gallery_category  as c 
LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id 
WHERE user='$user'
GROUP BY c.category_id" );

But when using this in a mysql query don't forget to make the mysql query one long line. Hope one of those examples works for ya.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

i did as u suggested,tested all the ideas and it was giving me this error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\viewgallery.php on line 23

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in C:\wamp\www\viewgallery.php on line 43


down is the part of the code how i did it

<?php session_start();
$user=$_SESSION['user'];
	include("config.inc.php");
	

	// initialization
	$result_array = array();
	$counter = 0;

	$cid = (int)($_GET['cid']);
	$pid = (int)($_GET['pid']);

	// Category Listing

	if( empty($cid) && empty($pid) )
	{
		$number_of_categories_in_row = 2;

		$result = mysql_query( "SELECT c.category_id,c.category_name,photo_filename,COUNT(photo_id) FROM gallery_category  as c LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id WHERE user='$user' GROUP BY c.category_id" );
						
		//$cool=mysql_query( "SELECT from gallery_photos WHERE photo_category ='$cid' ORDER BY photo_id DESC LIMIT 1 " );				
						
		while($row = mysql_fetch_array( $result ))// this is line 23
		{
		
		if($row[3]==0){
		//echo"no photos";
		//$result_array[] ="<img src=album/folder.png  width=100 height=80 border=0 alt=$user>";
		$result_array[] ="<tr ><td><table class=forumdisplay bgcolor=#ffffff><tr valign=top><td width=120><a href='viewgallery.php?cid=".$row[0]."'><img src=album/folder.png  width=100 height=75 border=0 ></a></td><td width=480 align=left><table><tr><td><a href='viewgallery.php?cid=".$row[0]."'>".$row[1]."</a></td></tr><tr><td>".$row[3]." photos </td></tr> <tr><td><a href='viewgallery.php?cid=".$row[0]."'>view album</a></td></tr></table></td></tr></table></td></tr>";
		
		}
		else
		{
		//$result_array[] = "<a href='viewgallery.php?cid=".$row[0]."'>".$row[1]."<img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."'/>"."</a> "."(".$row[3].")";
		 
		$result_array[] ="<tr ><td><table class=forumdisplay bgcolor=#ffffff><tr valign=top ><td width=120><a href='viewgallery.php?cid=".$row[0]."'><img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."'/></a></td><td width=480 align=left><table><tr><td><a href='viewgallery.php?cid=".$row[0]."'>".$row[1]."</a></td></tr><tr><td>".$row[3]." photos </td></tr><tr><td><a href='viewgallery.php?cid=".$row[0]."'>view album</a></td></tr></table></td></tr> </table></td></tr>";
		}
		
		
		
			
		}
		mysql_free_result( $result );	//this is line 43

i hope u can help me from here

mrcniceguy
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 17
Solved Threads: 8
 

What is the name of your user table? Is there a user_id column or something similar on the gallery_photos table?

TommyBs
Junior Poster in Training
61 posts since Mar 2008
Reputation Points: 11
Solved Threads: 11
 

i Succeeded !!
i just added this after the where clauses

WHERE c.user='$user'

instead of just user='$user'

mrcniceguy
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 17
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You