我正在尝试开发一对一聊天。我希望用户只能看到来自发件人的消息,即发件人只能看到来自他自己和接收的消息,但查询无法运行。我能够看到数据库中存在的所有消息
public function getMessages() {
$messages = array();
$query = <<<QUERY
SELECT
`chat`.`message`,
`chat`.`sent_on`,
`user1`.`id`,
`user1`.`first_name`
FROM `user1`
JOIN `chat`
ON `chat`.`user_id` = `user1`.`id` AND `chat`.`rec_id`=$recid
ORDER BY `sent_on`
QUERY;
答案 0 :(得分:0)
您需要在查询中指定userId
,如下所示:
public function getMessages($userId) {
$messages = array();
$query = <<<QUERY
SELECT
`chat`.`message`,
`chat`.`sent_on`,
`user1`.`id`,
`user1`.`first_name`
FROM `user1`
JOIN `chat`
ON `chat`.`user_id` = `user1`.`id` AND `chat`.`rec_id`=$recid
WHERE `user1`.`id` = {$userId}
ORDER BY `sent_on`
QUERY;