I working on a secured blog where I have a specific post displayed in separate page once a user clicks read more button. This separate page includes registered users comments.The issue I have is that the name of the user who wrote the comment is not matching the actual user, its showing instead a name of a user who wrote a post. My sql query:
$comsql="SELECT c.*,u.name FROM posts p JOIN users u ON p.user_id=u.id"
." JOIN comments c on p.id=c.post_id ORDER BY commentdate DESC";
My echo:] Written by: , On: ">Edit comment | ">Delete comment
Yeah, it's what you ask:
ON p.user_id=u.id
You should have somthing like this:
ON c.user_id=u.id
In your SQL query you're matching the user of the post instead of the user of the comment. Do you have a user_id field in the comments table ?
Probably, both comments and users tables have name field. To fix this;
Change query to:
$comsql="SELECT c.*,u.name AS username FROM posts p JOIN users u ON p.user_id=u.id"
." JOIN comments c on p.id=c.post_id ORDER BY commentdate DESC";
If doesn't work, please leave a comment
Related
i want to make comment in article. what i need is : username, comment, and id post than every post have different comment. i have 3 table, this below is my table name and it field :
user : id_user, username
posting : id_user, id_post
comment : id_com, id_user, id_post, comment
when i finish make it with my query, i check it by comment in user 1 post using user 2 account. what happend is, comment become posted by user 1.
i use this query below :
$query=$dbc->query("SELECT
user.username,
comment.id_com,
comment.comment,
posting.id_post
FROM comment
INNER JOIN (posting INNER JOIN user ON user.id_user =
posting.id_user) ON posting.id_post = comment.id_post
WHERE comment.id_post='$_GET[id]'");
I have three table tbl_user, tbl_posts, tbl_friends
tbl_user stores all the user datas.
tbl_friends stores which user are friends with other.
tbl_posts, where the users posts are stored. userid is the field where all the users id are stored. p_viewstatus field stores the view status of the posts. if the view status of the my friends posts is only by me then that posts should not shown to other users
If sam have a post with id=40 in tbl_posts. The p_viewstatus of the post is only by me then that post must only be visible to sam only.
If sam have another post with id=41 and p_viewstatus!='only by me' this post must visible to all users who are sam's friend
Tried the following query but don't know how to relate the p_viewstatus
Note: I want to fetch all of my posts and my friends posts from **tbl_posts except the p_viewstatus of my friends posts no equal to 'only by me'**
SELECT * FROM `tbl_posts` as p,`tbl_friends` as f WHERE p.`userid`=f.`userid` and p.`userid`=23
Thanks in advance.
BELOW SCREENSHOT SHOWS THE TABLE CONTENTS AFTER JOIN QUERY.
Here's how your query should look like:
SELECT * FROM `tbl_posts` as p
WHERE p.`userid`=23 OR (p.`userid` IN (SELECT `friendsid` `FROM tblfriends` WHERE `userid`=23)
AND p.`p_viewstatus`<>'only by me')
First you select the data from the table you need. After that you filter it with the user id and the ids of the friends of the user and you also tell it that you only want the posts that are not viewable only by me.
I have two separate tables. I need to SELECT the avatar from the user table, WHERE the username equals from on the comments table. I am trying to create a comment system that displays the user's avatar next to their message.
Comments - ID|Username|From|Timestamp|Message
User - ID|Username|Avatar
$fetchto=mysql_fetch_object(mysql_query("SELECT * FROM user WHERE username='$variable'"));
I think I could display the URL to the avatar using $fetchto->avatar if I had a variable that would pull the avatar of the member making the comment from the user table.
First off your database isn't properly normalized. The comments should refer to the User by UserId, not by Username. Once you've fixed that:
select * from Comments c
join User u on u.ID = c.UserId
Until then:
select * from Comments c
join User u on u.UserName = c.UserName
Also, please stop using the mysql_ family of functions - they're deprecated.
Your query needs to have a simple join, something like this:
SELECT c.*, u.avatar
FROM comments AS c
JOIN user AS u ON c.username = a.username
I'm trying to display #mentions for my currently logged in user but I'm getting a syntax error and I don't know what it is. My database is mysql and my query currently looks like this:
$sql = "SELECT
users.username,
posts.post,
posts.time_stamp,
FROM users
INNER JOIN posts
ON users.id = posts.user_id
INNER JOIN mentions
ON users.id = mentions.user_id
WHERE mentions.user_id = '$userid'
AND mentions.unread = 1
ORDER BY time_stamp DESC
";
What I want to happen is for the currently logged in user to go to a page called "mentions" and to get a list of the posts they were mentioned in and the username who mentioned them BUT ONLY if the mention is unread (i.e. is equal to 1) because later on that same page I will then change the displayed mentions 'unread' status to 0, which means it has been read.
I hope this makes sense! If you need more info on the db structure etc, shoot a comment and I will give you more info.
You have at least one syntax error in this line:
posts.time_stamp,
Remove the comma. It is not needed before from.
Okay, so I want to have a news feed on my website. I have 3 tables named Users, Follow, and Posts. Basic user data goes into the Users table, who is following who is stored in the Follow table, and whatever the user posts goes into Posts. Now, I know how to select every post from a database table and limit it using the WHERE clause. But how would I write my query to select all all of the posts from only user's they are following, and display them by DESC date? Thanks.
Here's a general layout for you, make three tables like you mentioned, I've outlined below just as an example
[TABLES]
users
followers
posts
In the users table you should have at least columns like userid (auto incremented value / primary key).
The followers table should have something like userid which would match to the users table userid, a followid column which would also have the id # for the follower from the users table.
Then for your posts table you would want to have userid too so when each post is made it would have the id from users table.
Then you would need to do something like:
SELECT p.*
FROM posts AS p
WHERE p.userid IN (SELECT followid FROM followers WHERE userid = ###)
ORDER BY p.date DESC
Now it really depends on how you are getting the users id to figure this out. If your passing the users id using a session after they logged in similar to something like Facebook, then you could change userid = ### to something like userid = ".$_SESSION['userid']." But again it really depends on how you pass the users id but the above should at least get you somewhat started.
Make sure to put indexes on the userid, followid columns so that when the table becomes larger it will do the joins quickly.
An alternative to #Shane's answer is to use the JOIN operator:
'SELECT p.* // I prefer to name all the fields, but for brevity's sake I'll use the shorthand
FROM Posts AS p
INNER JOIN Follow AS f ON p.userid = f.followid
WHERE f.userid = '.$selectedUserID.'
ORDER BY p.date DESC;'
For an inputed User ID ($selectedUserID), it will find all User ID's of the people they follow (matching follow ID to user ID on the Follow x-ref table) and then find their respective posts from the Post table in descending order by date. It will return empty if they do not follow anyone or the people they follow have no posts.
I also hope I do not need to remind you to sanitize your input to the database and escape your output to the web.
Is this what you're looking for?