尝试仅返回每个UserId的一行(最新)行(如果包含在MySQL数据库中),以使结果显示"聊天记录"每说。基本上类似于任何其他用户的最新消息的测试历史概述。
<?php
$userId = $_POST['userId'];
$pdo = new PDO('mysql://hostname=localhost;dbname=data_base', 'user', 'password');
$query = $pdo->query("SELECT * FROM `messages` WHERE `from_id`='{$userId}' OR `to_id`='{$userId}' ORDER BY `stamp` DESC");
$data = $query->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data);
答案 0 :(得分:0)
<?php
$userId = $_POST['userId'];
$pdo = new PDO('mysql://hostname=localhost;dbname=data_base', 'user', 'password');
$query = $pdo->query("SELECT * FROM `messages` WHERE `from_id`='{$userId}' OR `to_id`='{$userId}' GROUP BY `from_id`, `to_id` ORDER BY `stamp` DESC");
$data = $query->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data);