dongxingguo1978 2010-09-09 17:15
浏览 28
已采纳

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.

  • 写回答

3条回答 默认 最新

  • doushua7737 2010-09-13 11:53
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改