开发者

PHP: Search in one table and in another

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 i开发者_如何学Cf 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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜