mysql friendlist table structure - php

I created an app and I want to add the feature that users can add other users to their "friend list".
I dont know how to create a mysql table to store this data, is this a good structure:
A table with 2 rows, one unique row "user" and one "friends", and in the friends row I will add the usernames of the friends like "John, Thomas, Sam, Peter".
And when I want to display the friend list, I will just search for the "user" and display all entries from "friends" row.

The best solution would be to make two tables.
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(10) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `friends` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`user_id` int(10) NOT NULL,
`friend_id` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
If you want to add a friend just add the unique user id from users table and the person who you want to be friends with on the friends table.
If you want to get all the friends:
SELECT `users`.`username` FROM `friends` LEFT JOIN `users` ON `friends`.`friend_id` = `users`.`user_id` WHERE `friends`.`user_id` = '1' // with specific user id
It will return the friends of user_id 1
$rs=$conn->query("SELECT `users`.`username` FROM `friends` LEFT JOIN `users` ON `friends`.`friend_id` = `users`.`user_id` WHERE `friends`.`user_id` = '1'");
$arr = $rs->fetch_all(MYSQLI_ASSOC);
foreach($arr as $row) {
echo $row['username'];
}

Related

How to select ONLY posts from friends and viewing user

I'm making a social plugin for my website, and I have a friends table that holds all accepted friend requests, and I need to display all posts from the users friend AND the users posts in order of the date, so I've tried this sql query:
SELECT DISTINCT `social_posts`.*, `social_friends`.*, `users`.*
FROM `social_posts`
JOIN `social_friends`
ON `fUID` = '1' AND `friend` = `pUID` OR `pUID` = '1'
JOIN `users`
ON `friend` = `uid`
ORDER BY `date` DESC
Structure
CREATE TABLE `social_friends` (
`fID` int(11) NOT NULL AUTO_INCREMENT,
`fUID` int(11) NOT NULL,
`friend` int(11) NOT NULL,
PRIMARY KEY (`fID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `social_posts` (
`pid` int(11) NOT NULL AUTO_INCREMENT,
`pUID` int(11) NOT NULL,
`body` text NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
fUID is the users (viewing) user ID, and friend is the users friend, and pUID is the user ID of the user who made the post.
But this shows two of each post, even with SELECT DISTINCT, and I'm out of ideas on how to figure this out.
1 - Can you give as more info (the fields) of the tables?
What is fUID and pUID
2 - Try and change
ON `fUID` = '1' AND `friend` = `pUID` OR `pUID` = '1'
to ON friend = pUID and put fUID = 1 OR pUID = 1 in the WHERE clause

PHP something wrong with while loop

Okay so now its display results like 3 times in a row
$user_apps = mysql_query("SELECT a.name,a.download_url FROM user_apps as ua LEFT JOIN
apps as a ON (ua.app_id=a.app_id)
WHERE ua.user_id='$user_id'") or die(mysql_error());
while($raw = mysql_fetch_array($user_apps)){
$name = $raw['name'];
$url = $raw['download_url'];
echo $name;
echo "<br />";
echo $url;
}
Database Table Structure(since I am new to the site and did not know how to display the table structure I just exported the sql)
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
CREATE TABLE IF NOT EXISTS `user_apps` (
`user_id` int(11) NOT NULL,
`app_id` int(11) NOT NULL,
KEY `user_id` (`user_id`,`app_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `apps` (
`app_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`description` text NOT NULL,
`icon` varchar(255) NOT NULL,
`download_url` varchar(255) NOT NULL,
`default` int(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`app_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
I'v tried different Join types but that does not seem to work.
Used the join query for get the result check bellow example query
$user_apps = mysql_query("SELECT DISTINCT a.name,a.download_url FROM user_apps as ua LEFT JOIN apps as a ON (ua.app_id=a.app_id) WHERE ua.user_id='$user_id'") or die(mysql_error());
while($raw = mysql_fetch_array($user_apps)){
$name = $raw['name'];
$url = $raw['download_url'];
echo $name;
echo $url;
}
change the join type as per your requirement. the above query for only example
INNER JOIN: Returns all rows when there is at least one match in BOTH
tables
LEFT JOIN: Return all rows from the left table, and the matched rows
from the right table
RIGHT JOIN: Return all rows from the right table, and the matched
rows from the left table
FULL JOIN: Return all rows when there is a match in ONE of the tables
more about join click here
AND also check this http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
You have used single quotes in query at user_id='$user_id' .
Are you sure your user_id is char, varchar or text? Just print_r($user_apps) and check it has any records or not? If user_id is int,tinyin than remove single quote.

Cannot retrieve column information from database

I have two tables, namely user_info and comments. I want to join the tables, so i can relate the user_id from the from comments after the inner join has happened, which will join based on the id columns. The whole reason for this is so i can find out what the id is of a user who posts a comment on a users profile, which i store in user_id.
SHOW CREATE TABLE user_info
CREATE TABLE `user_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(12) COLLATE utf8_unicode_ci NOT NULL,
`pass` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`joined` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`))
ENGINE=InnoDB AUTO_INCREMENT=64 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
SHOW CREATE TABLE comments
'CREATE TABLE `comments` (
`id` int(11) DEFAULT NULL,
`comment` text COLLATE utf8_unicode_ci,
`date_posted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` int(11) DEFAULT NULL)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
The query code
try {
$result = $db->prepare("SELECT user_id
FROM comments
INNER JOIN user_info USING (id)
WHERE id = :user");
$result->bindParam(':user', $post_session_user_id);
$result->execute();
$comment_details = $result->fetch();
}
How comment_details is stored
$comment_id = $comment_details[0];
and then i do a var_dump that returns NULL
var_dump($comment_id);
Any ideas what is wrong?
I think you should not be using USING as that works only when you have 2 fields, one in each table both having the same name.
In your tables comment.user_id holds the user_info.id
So your query should probably be
SELECT user_id
FROM comments
INNER JOIN user_info ON user_info.id = comments.user_id
WHERE comments.id = :user

How do I query the latest save progress of a list of users. (php/html)

I have a register and login site where people can click save progress on a form and it inserts a new row with their session id in column 2 as (user_id).
I also have an admin login where I can see their entries but it shows a list all their saves.
I wonder if you could help me figure out a query to list all the latest save progresses of each unique person? like (id,user_id,name,score): (3,3,bob, score 5) (6,4,sam, score 30) without showing all saves of a user's past saves like:
(1,3,bob, score 5) (2,4,sam, score 30) (3,3,bob, score 5) (4,4,sam, score 30)
I need the latest save of each user.
Like the latest id of a distinct list of user_id. Hope this makes sense. Thanks!
select user_id,name,score from your_table where (user_id,id) in (select user_id,max(id) from your_table group by user_id)
Considering the below formats for your tables
CREATE TABLE IF NOT EXISTS `user` (`user_id` int(11) NOT NULL auto_increment,
`user_name` varchar(200) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `user_score` (
`id` int(11) NOT NULL auto_increment,
`user_id` int(11) NOT NULL,
`user_score` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1;
Please try executing the below sql select query for retrieving latest score for each user
SELECT u . * ,s.user_score,s.id FROM user u JOIN user_score s ON u.user_id = s.user_id WHERE id IN (SELECT MAX( id ) FROM user_score GROUP BY user_id)

How do I log in and display information from two tables (MySQL)?

I'm new to MySQL and PHP so Im not sure how to approach this problem I'm having.
I have two tables right now.
CREATE TABLE `users` (
`userid` int(25) NOT NULL AUTO_INCREMENT,
`username` varchar(65) NOT NULL DEFAULT '',
`password` varchar(32) NOT NULL DEFAULT '',
`emailaddress` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`userid`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
and
CREATE TABLE `images` (
`userid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`image` blob,
PRIMARY KEY (`userid`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
so what I want to do is when a user signs in I want to be able to display an image that the user uploaded.
do I have to do something to the tables to make theme reference from each other?
help please!
Do you want just?...
select image from images
left join users on users.userid=images.userid
where username='whateverusername';
in the second table , the attribute userid should be a foreign key (i'd rather use Innodb to make sure that there is a foreign key constraint but it's up to u to use innodb or not)
so your table should look like this
CREATE TABLE images ( userid int(10) unsigned NOT NULL, name
varchar(50) DEFAULT NULL, image blob, foreign key userid
references users(userid) on delete cascade ) ENGINE=InnoDB
AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
once you do that, the table images will be linked to the table users which means that no record will be added to the table images unless the user id is already in the table users
if you wanna grab all the informations about that users including the image , you can perform a join between the two tables.
example with php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$user_id = 1;
$results = array();
> $results =mysql_query("select t1.userid,t1.username,t2.name,t2.image from users as t1 left join images as t2 on t1.userid=t2.userid where userid = $user_id",$con);
UPDATE:
make sure that the type of userid in both tables match

Categories