简体   繁体   中英

How to get user's friends data in php and MySQL?

I have a problem on getting user's friend data, As you know that When you loggedin in facebook and open yours friend profile you will able to see all the latest updates, photos of your friends. Just like that i want to make same thing, I have some database tables like Photos and crop(information table)

So I want to know how to get data from 2 different tables or more?

Here is my Code:

if (isset($_GET['username']) === true && empty($_GET['username']) === false) {
    $username = $_GET['username'];
if (user_exists($username) === true && friend_exists($username) === true) {
    $user_ids = user_id_from_username($username);
    $friend_ids = user_id_from_friend($username);
    $profile_data = user_data($user_ids, 'user_id', 'username', 'password', 'first', 'last', 'mail', 'mobile', 'gender', 'email_code', 'active');
    $friend_photo = user_photos($user_ids,'photo_id','user_id','profile','username');

    echo $friend_photo['username'].'<br>';
    echo $profile_data['username'];

Functions:

function user_id_from_username($username){
    $username = sanitize($username);
    return mysql_result(mysql_query(" SELECT `user_id` FROM  `crop` WHERE `username` = '$username'"), 0, 'user_id');
}
function user_id_from_friend($username){
    $username = sanitize($username);
    return mysql_result(mysql_query(" SELECT `user_id` FROM  `photos` WHERE `username` = '$username'"), 0, 'user_id');
}

 function user_exists($username){
    global $username;
    $username = sanitize($username);
    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `crop` WHERE `username` = '$username'"), 0) == 1) ? true : false;
}
function friend_exists($username){
    global $username;
    $username = sanitize($username);
    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `photos` WHERE `username` = '$username'"), 0) == 1) ? true : false;
}

How can I solve this problem? Any tips, tech, if I have to change codes, let me know please.

Since you have user_id as your primary key for users table and you have user_id in photos as reference to your users user_id. That is typically called One to Many relationship, this means you have one user_id which owns many photos . Since I am not sure how your database is design and structured in MySQL, but I can assume that you can use some thing like LEFT JOIN .

In MySQL it can be done like this:

SELECT * FROM users
    LEFT JOIN photos ON
    users.user_id = photos.user_id
WHERE users.username = 'theUserName';

This statement will show all photos belong user_id = 1 .

We can write this as statement in our PHP like this:

"SELECT * FROM `users` LEFT JOIN `photos` ON users.user_id = photos.user_id WHERE `username` = '$username'"

Now if you want to allow showing photos of one user to other users, you need to make many to many relation (pivot table) by creating a new table called users_friends or some thing like that.

In this table you create two rows one called user_id and the other friend_user_id so each user_id will allow friend_user_id to view own photos.

This is the way it works.

Try it and let me know, if this was what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM