Page 1 of 1

can't get the post to show to the user only

Posted: Sun Sep 16, 2012 12:19 pm
by marytan
I have a function and can't make it work. I tried many things but still no luck.
function get_posts($id = null, $category_id = null) {
	$posts = array();

	$query = "SELECT  `posts`.`id` AS  'post_id',  `categories`.`id` AS  'category_id', 
			`title` ,  `content` ,  `date_posted` , `categories`.`name` 
			FROM  `posts` 
			INNER JOIN  `categories` ON  `categories`.`id` =  `posts`.`category_id`";
			
	if (isset($id)) {
	$id = (int)$id;
	$query .= "WHERE `posts` . `id` = '{$id}'";
	}
	
	if (isset($category_id)) {
		$category_id = (int) $category_id;
		$query .= "WHERE `category_id` = '{$category_id}'";
	}
	
	$query .= "ORDER BY `posts` . `id` DESC";
			
	$query = mysql_query($query);
	
	while ($row = mysql_fetch_assoc($query)) {
		$posts[] = $row;
	}
	return $posts;
}
How do I make it show only to the user who posted it, not to all the users. It's supposed to work like a diary. :?

Re: can't get the post to show to the user only

Posted: Sun Sep 16, 2012 9:07 pm
by jacek
You can add a WHERE to the quesry yo fetch only a certain users posts
SELECT `things` FROM `table` WHERE `user_id` = {$_SESSION['user_id']}
Something along those lines ?