简体   繁体   English

PHP从MySQL获取用户数据

[英]php fetch user data from mysql

I currently have 2 tables in a MySql database. 我目前在MySql数据库中有2个表。

Structure: 结构体:

messages
id | content | user_id | time

submessages
id | subcontent | msg_id | user_id | time

In the submessages table, column msg_id is a foreign key, referencing the id column in table messages . 在子submessages表中, msg_id列是外键,引用表messagesid列。

Now i need to query from php 现在我需要从php查询

$cur_userID = $user->current_id; // current user id.
SELECT * FROM messages WHERE id > '{$_GET['id']}' // problem here

How do I query the submessages table for subcontent that was posted by other users - exlcuding current user? 如何在subcontent submessages表中查询其他用户发布的subcontent -排除当前用户?

Meaning anything with a user_id equal to $cur_userID shouldn't be displayed. 意味着任何user_id等于$cur_userID都不应显示。

Thank you. 谢谢。

You want to get the submessage detail, right? 您想获取子消息详细信息,对吗? This query would get all the info from the message table, plus all the info from EACH of the sub-messages. 该查询将从消息表中获取所有信息,以及从每个子消息的EACH中获取所有信息。 Thus the join. 因此加入。 But it would exclude sub-messages (answers?) that have the same user_id as the messages (questions?). 但是它将排除与消息(问题?)具有相同user_id的子消息(问题?)。

SELECT m.* sm.*
FROM messages m
INNER JOIN submessages sm
  ON m.id = sm.msg_id
WHERE id > '{$_GET['id']}'   // dont know why gt is used here...
  AND m.user_id <> sm.user_id

And I ignored all the formatting necessary to put this into PHP, since really the question as I understand it is how should the SQL be written. 而且我忽略了将其放入PHP所需的所有格式设置,因为据我了解,真正的问题是应如何编写SQL。

As an aside, I don't get the id > $_GET['id'] section. id > $_GET['id'] ,我没有得到id > $_GET['id']部分。 It appears that you are asking for all messages created AFTER this message, and you want to find the submessages for thos messages. 似乎您要询问在此消息之后创建的所有消息,并且您想查找该消息的子消息。 I really can't figure out when this is reasonable, but if that is what you want this is a way that should work. 我真的不知道什么时候是合理的,但是如果那是您想要的,这就是应该起作用的方法。 I suspect you want equals , not greater than . 我怀疑你要平等相待 ,不大于

"SELECT * FROM messages WHERE id != " . intval($_GET['id']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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