PHP: Search in one table and in another - php

I am making an autosuggesting function, when the user writes something in the field it stores it in:
$queryString = $db->real_escape_string($_POST['queryString']);
I want it to autosuggest after the users friends. The user´s friends is in users_friends, but only the friend´s ID. Now their full_name is in the table "users". And i want when you search it should in users for the full_name + check if its friends with the user.
As you may understand i do not expect all my users to know eachother id´s so writing e.g "52" "233", but searching for their full_name s.
UPDATE:
I know tried doing this:
$query = $db->query("SELECT uf.bID
FROM users friends, users_friends uf
WHERE uf.uID = '1' AND uf.type = 'friend' AND friends.full_name LIKE '$queryString%' LIMIT 10;"
);
It selects the bID, from the users friends WHERE the userid is 1 and are friend.
Now i start to see some results i think. When i write a full_name that im friends with, i get the id of the user(the id that is stored in bID). Now i just need to grab the full_name in "users" where id = bID..
table: users
id | full_name
table: users_friends
id | uID | bID
So conclusion of all this (trying to make a better summary in order to make you understand better: )
When you type in e.g Jack in the search field, then the $queryString is now "jack". Then it is taking "Jack"(full_name in users), grabbing his id(id in users), if he exists there ofcourse, and then match it with bID (in users_friends) where uID is $USER; ($user is the current user that are logged in´s id.)
Hope this was easier to understand, please leave comment if theres something unclear.

So, as i figure it out, you've got the current user's id in $USER and its query string in $queryString, and what you want is the names of the user's friends based on the $queryString, am I right?
So, assuming the database's schema is as you've put:
table: users
id | full_name
table: users_friends
id | uID | bID
See if this query works out for you, then:
SELECT users.full_name
FROM users INNER JOIN users_friends ON users.id=users_friends.uID
WHERE bID=$USER AND users.full_name LIKE '$queryString%'
LIMIT 10;
Where $USER and $queryString are your variables.

Do you want to read data from many tables at once??
SELECT table1.id, table2.name FROM table1, table2 WHERE ...
My english is not very good to understand everything :D

I didn't understood your question
But you can use this query:
mysql_query("SELECT * FROM table_one, table_two WHERE table_one.id = table_two.id");

Related

How to check if a user follows another user from a list of users?

I'm creating a follow system in PHP where users can follow/unfollow other users. So far, when a user follows any other user, the id of both the users are stored in 'follow' table in MYSQL database,
u_id | f_id
===========
1 | 2
1 | 3
1 | 5
Where u_id follows f_id
Now when u_id 1 performs a search on the website, all the users with matching search query are presented, each having a button so that u_id 1 may follow them, but there may be some users that are already followed by u_id 1. How do I implement such that those users already followed by u_id 1 does not have that follow button?
One way is to check every result if they are followed by u_id 1 before presenting the search result, but I guess this will be very slow.
Is there any way so that I can do this in a single MYSQL query?
when u_id = 1 performs a search on the website, all the users with matching search query are presented
Looks like you have some kind of query returning user_id and a suggested user f_id to follow. Let assume that is a table suggestion
SELECT s.f_id,
CASE
WHEN f.id IS NULL THEN 'ON'
ELSE 'OFF'
END as ButtonStatus
FROM [suggestion] s
LEFT JOIN [follow] f
ON s.user_id = f.u_id
AND s.f_id = f.f_id
WHERE
s.user_id = #user_id // Variable from your UI.
$sql = "SELECT u.*, f.followingUsersId AS followingUsersId
FROM users AS u LEFT JOIN (SELECT followingUsersId FROM follow WHERE followerUsersId = '".$_SESSION['userid']."') AS f ON u.usersId = f.followingUsersId
WHERE u.usersName LIKE '%$searchvalue%' ORDER BY f.followingUsersId DESC, u.usersId DESC;";
Worked for me!!

making the relationship through username and not id

So Lets say I have a user table and then a user_profile table. To connect the relationship there would be a user_id row within the user_profile table. Now when I'm building my applications I like to make my url show the username and not the user_id example:
http://www.example.com/username/profile
not:
http://www.example.com/user_id/profile
So what I find myself doing is getting the user_id through the username and then fetching the profile info which just adds an extra query for no reason. My questions is could I just make the relationship through the username which is just as unique as the id row in the users table. Or is this bad practice and I should stick with using user_id?
A much better idea would be to use the user_id to join the tables, but provide the username as a parameter. Something like:
select p.*
from users u
, profiles p
where u.id = p.user_id
and u.username = ?

Follow friend of friends suggestion MySQL

I'm creating a 'Follow Suggestion' feature, where I'd like to show random Friends of my Friends who's NOT my friend.
user_friends table
friend_id | friend_one | friend_two | role
user table
uiD | username
For each friend that a user follow it makes two records. If user 1 and 2 become friends a record would be created where, friend_one = 1 & friend_two = 2, friend_id = ramdom AUTO_INCREMENT number role = fri in the user_friends table.
User table, just has the users id and username.
How would I make a sql query that suggests "who to follow" based on friends of my friends ? Just how twitter does it.
What I've tried hasn't quite worked which does not seem to make much sense even to me.
http://pastebin.com/tCt6jdAZ <- Query code. ( Don't want to post here because feels useless ).
If I understand your question correct:
you need to join your 'user_friends' table twice:
select * from
user_friends level1,
user_friends level2
where
level1.friend_two = level2.friend_one -- or opposite i am not sure if a understood your model
and level1.friend_one = 'starting friend id'

Get mutual friends from Facebook friends (MySQL table)

I'm trying to get mutual friends between 2 people.
I've saved the persons friends in a table called "friends" with the following fields:
id | facebook_id | name | uid | timestamp
id = unique id for the record
facebook_id = the friends facebook id
name = the friends name
uid = the users uid on my site, which is a friend to the person saved in the table
timestamp = don't need to explain :-)
Hope it make sense, have tried various ways to get the friends, but without luck
I dont know if using subqueries would be faster or mySQL already optimizes it.
Solution with subqueries:
SELECT f1.id, f1.name
FROM (SELECT id, name FROM friends WHERE uid=1) f1
JOIN (SELECT id, name FROM friends WHERE uid=2) f2
ON f1.id=f2.id;
It would be nice if MySQL has an INTERSECT operator, but...
You can grab all the friends of one person:
SELECT id, name
FROM friends
WHERE uid = 1
You could then JOIN this list back to the friends table, looking for the same friend for the other user:
SELECT f1.id, f1.name
FROM friends f1
JOIN friends f2 on f1.id = f2.id
WHERE f1.uid = 1
and f2.uid = 2
It's pseudo-code, but it should be close.

Get users position between friends

I'm trying to get a users position between the users friends, but I don't have any idea of how I can do this...
I have two tables.
Table 1: friends (where all the users friends are listed)
Table 2: users (where all the users are listed)
I want the query to check the users position between his friends.
So if I, for example have ID 1 (with 100 credits) and a friend with ID 2 (with 21 credits), the query would list my position as 1.
You don't really provide much information on your table layout, so it's going to be impossible for me to provide a very specific example. I'm also afraid I don't really understand your question, but I'll give it a shot...
First, I'll assume your users table has at least these columns:
id (PK)
credits
And that the friends table has these columns:
user (FK to users.id)
friend (FK to users.id)
Now, if I understand your question, you want to rank all of a user's friends, based on how many credits they have, so:
SELECT u.id,u.credits
FROM friends AS f
JOIN users AS us ON f.friend = u.id
WHERE f.user = 1
ORDER BY u.credits DESC;
in order to get the position I would recommend using PHP for this and not try to put it all in one query. So get a sorted list like Flimzy described and get the position by using an array function like array_search.

Categories