Can't join third table in php/mysql to get user name - php

I have a join that I have working perfectly. However, I need to join to a third table called wp_users so I can access the user display name. When I add a second join I keep getting all sorts of errors depending on what I try so I'm turning for help.
The third table, wp_users has two columns:
wp_users.ID,
wp_users.display_name
Here is the code before I add the second join code so I can get the display name for the results ID
//Works, but I need to join to wp_users on wp_users.ID to get display_name
$results = $dbh->prepare("select
stories.ID,
stories.SID,
stories.story_name,
stories.category,
points.ID,
points.PID,
FROM stories
JOIN points ON stories.SID=points.SID
JOIN stories ON wp_users.ID=points.ID
where (points.ID = $user_ID) and (PID = 1)");
$results->execute();

Change stories to wp_users
JOIN stories ON wp_users.ID=points.ID
to
JOIN wp_users ON wp_users.ID=points.ID
$results = $dbh->prepare("select
stories.ID,
stories.SID,
stories.story_name,
stories.category,
points.ID,
points.PID,
FROM stories
JOIN points ON stories.SID=points.SID
JOIN wp_users ON wp_users.ID=points.ID
where (points.ID = $user_ID) and (PID = 1)");
$results->execute();

Related

Mysql select nickNames from other table?

I have 3 tables for a chat app
chatRooms (id)
chatMessages (id, roomId, userId, message)
userData (userId, nickName)
How can I load all the chatRooms with the chatMessages with the nickNames?
I tried:
$qry = $db->prepare('SELECT
chatRooms.id,
chatMessages.message,
userData.nickName
FROM chatRooms
LEFT JOIN chatMessages
ON chatMessages.roomId = chatRooms.id
LEFT JOIN userData
ON chatMessages.userId = userData.userId '
);
$qry->execute();
But it doesn't seem to work for me:C
I would like to display all the users who are in the chatRoom in the chat name
So if 3 people (Fred, Joe, Bane) are in the group then I somehow would need an array with them. For every array element(chatRoom)
Your first LEFT JOIN is more than likely returning more than one row.
From your question, your "chatMessages" table also has a userId in it. You should also filter by this.
In order to do this, you would need to:
Join userData before chatMessages
Include the userId from userData in the LEFT JOIN on chatMessages
$qry = $db->prepare('SELECT
chatRooms.id,
chatMessages.message,
userData.nickName
FROM chatRooms
LEFT JOIN userData
ON chatMessages.userId = userData.userId
LEFT JOIN chatMessages
ON chatMessages.roomId = chatRooms.id AND chatMessages.userId = userData.userId');
$qry->execute();

SQL query to fetch data of a particular user, from three tables

First is a user table, second a places table and third a favorites table (having two fk, one from user and another from places). I want to get all the favorite places of one user, how do I do that?
I tried to play around with joins but what I don't know is how to get data of a particular user.
$qry =
"SELECT places.place_id , places.db_image ,places.description from places
inner join favorites on places.place_id = favorites.place_id
inner join user on user.id = favorites.user_id
";
$query=mysqli_query($con ,$qry);
$return_arr = array();
$sql = sprintf(
'SELECT
p.place_id,
p.db_image,
p.description
FROM
places p INNER JOIN
favorites f ON p.place_id = f.place_id INNER JOIN
user u ON u.id = f.user_id
WHERE
u.id = %d'
, $userId);
or if you want simply select all columns from places
SELECT
p.*
FROM
places p INNER JOIN
favorites f ON p.place_id = f.place_id INNER JOIN
user u ON u.id = f.user_id
WHERE
u.id = %d
It is better to use prepared statements, but if you are not familiar with them, this is good enough.

database - Inner join with 3 tables in mysql issue

I'm doing a favorite system which allow users to save the posts to their page.
The tables I'm using are:
saved_posts table which contain 3 columns (painned_id``user_id``post_id)
users which contain (user_id, frist_name, last_name, username, email, password, user_website, user_avatar)
Finally posts table (post_id, user_id, post_author, category_id, post_date, post_image, post_avatar, user_website, post_keywords, post_content)
The my sql code I'm using to get the saved posrt
SELECT * FROM posts INNER JOIN saved_posts
ON saved_posts.post_id =
posts.post_id INNER JOIN users on saved_posts.user_id = users.user_id WHERE
saved_posts.user_id = users.user_id AND saved_posts.post_id = posts.post_id
The problem is that the saved posts from one user appears for other user as if they saved them as well.
Shouldn't you filter your answer with a WHERE condition? Otherwise it is just finding all posts that any user shared EVER.
SELECT * FROM posts
INNER JOIN saved_posts
ON saved_posts.post_id = posts.post_id
INNER JOIN users
ON saved_posts.user_id = users.user_id
WHERE users.user_id = :wanted_user_id
Don't forget to bind wanted_user_id.
If I understood the question correctly, the task is to select all the posts which of the users
SELECT * FROM posts p
INNER JOIN saved_posts sp ON p.post_id = sp.post_id
and sp.user_id = u.user_id
INNER JOIN users u on p.user_id = u.user_id

Difficulty with join 3 tables in a query in php

My database has 3 tables i wish to access in the select query but I cannot seem to get it to work. Selecting from 2 tables works fine so I know everything else is working apart from my code for selecting from 3 tables. My database has been created on PHPmyadmin
The Tables are as follows:
forum_replies
reply_id
topic_id
user_id
reply_text
reply date
forum_topics
topic_id
category_id
user_id
topic_title
topic_description
topic_date
users
user_id
username
This is the code I have tried to use and shows the fields I wish to select:
$queryreply = "SELECT forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id,
forum_replies.reply_text, forum_replies.reply_date, users.user_id, users.username
forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date
FROM forum_replies
JOIN forum_topics
ON forum_replies.topic_id = forum_topics.topic_id
JOIN users
ON forum_replies.user_id = users.user_id
";
$result = mysql_query($queryreply) or die (mysql_error());
$row = mysql_fetch_array($result);
Example in code would be appreciated. Thanks
Use this query:
SELECT a.reply_text, a.reply_date, b.topic_title, c.username
FROM forum_replies a
LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
LEFT JOIN users c ON a.user_id=c.user_id
// apply WHERE, ORDER, GROUP if needed
Apart from syntax errors, you should use LEFT JOIN and table alias in your case.
To show also the topic creator's username, you can adjust the query to the following:
SELECT a.reply_text, a.reply_date, b.topic_title, c.username AS reply_user, (SELECT username FROM users WHERE user_id=b.user_id) AS topic_creator
FROM forum_replies a
LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
LEFT JOIN users c ON a.user_id=c.user_id
// apply WHERE, ORDER, GROUP if needed
You miss the , after users.username..
SELECT forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id,
forum_replies.reply_text, forum_replies.reply_date, users.user_id, users.username,
forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date

Problem with SQL statement in MySQL

I am running an SQL statement:
$sql = "SELECT pic, userid
FROM user_details
INNER JOIN wp_users ON user_details.userid
WHERE wp_users.id = user_details.userid
LIMIT $recordstart, $pagesize
";
The query is meant to select records from user_details and wp_users, but should only select if there is a match (wp_users.id = user_details.userid).
The target is for it to basically select from a table while retrieving a record (based on the id match) from the other table
The problem is that it shows duplicate records. How can I fix this problem?
You should put your JOIN condition after ON. Like this:
$sql="select pic, userid
from user_details
inner join wp_users on (wp_users.id=user_details.userid)
limit $recordstart, $pagesize";
But the real problem is that you have more then 1 record in user_details for each corresponding record in wp_users (or vise versa).
INNER JOIN is causing database to make a Cartesian product of user_details and wp_users records. Then result table is filtered by your current WHERE condition (wp_users.id=user_details.userid). Depends on your needs you can use GROUP BY or DISTINCT if you want to retrieve unique records only.
For example, if you need userid to be unique:
$sql="select pic, userid
from user_details
inner join wp_users on (wp_users.id=user_details.userid)
group by userid
limit $recordstart, $pagesize";
SELECT DISTINCT pic, userid
FROM user_details
INNER JOIN wp_users ON wp_users.id=user_details.userid
LIMIT $recordstart, $pagesize
Use DISTINCT to return only unique rows and complete your join condition in the ON section:
$sql="select distinct pic, userid
from user_details
inner join wp_users on wp_users.id=user_details.userid
limit $recordstart, $pagesize";
use GROUP BY , because it shows duplicate records if there is multiple records in second table per on one record in first table
You left out the = part of the ON by accident.
$sql="select pic, userid
from user_details
inner join wp_users on user_details.userid
= wp_users.id
where wp_users.id=user_details.userid
limit $recordstart, $pagesize";
I might try
$sql = "select pic, userid
from user_details
inner join wp_users on user_details.userid = wp_users.id
limit $recordstart, $pagesize";

Categories