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

Post here if you need help with SQL.
Post Reply
marytan
Posts: 9
Joined: Sun Sep 09, 2012 11:25 am

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

Post by marytan »

I have a function and can't make it work. I tried many things but still no luck.

[syntax=php]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;
}[/syntax]

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. :?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post by jacek »

You can add a WHERE to the quesry yo fetch only a certain users posts

[syntax=sql]SELECT `things` FROM `table` WHERE `user_id` = {$_SESSION['user_id']}[/syntax]
Something along those lines ?
Image
Post Reply