gpt4 book ai didi

php - 如何使用 json 选择

转载 作者:行者123 更新时间:2023-11-29 23:00:02 25 4
gpt4 key购买 nike

<?php

require("config.inc.php");

if (!empty($_POST)) {
$query = "SELECT * FROM chatt
WHERE
fromm= :username AND chatt.too IN
(SELECT friend2 FROM friends where friend1=:username)
";

$query_params = array(
':username' => $_POST['username'],

);


try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$rows = $stmt->fetchAll();


if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"] = array();

foreach ($rows as $row) {
$post = array();


$post["Message"] = $row["message"];
array_push($response["posts"], $post);
}

echo json_encode($response);


} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}

} else {
?>
<h1>Login</h1>
<form action="selectchat.php" method="post">
Username:<br />
<input type="text" name="username" placeholder="username" />
<br /><br />


<input type="submit" value="Login" />
</form>
<a href="register.php">Register</a>
<?php
}

?>

上面的代码给了我这个结果:

{"success":1,"message":"Post Available!",
"posts":[
{"to":"ezenwa","Message":"this is me yes","time":"2015-02-08 23:47:31"},
{"to":"mark","Message":"ok i can see","time":"2015-02-08 23:47:31"},
{"to":"ezenwa","Message":"seems good","time":"2015-02-12 17:44:10"},
{"to":"john","Message":"j nman ","time":"2015-02-13 14:42:16"},
{"to":"david","Message":"j nman ","time":"2015-02-13 14:43:28"},
{"to":"john","Message":"this is cool","time":"2015-02-13 14:58:28"}
]}

但我希望它只显示用户与其列表中的每个人之间的最后一条消息,例如......

编辑:更新json格式。

{"success":1,
"message":"Post Available!",
"posts":[
{"to":"mark","Message":"ok i can see","time":"2015-02-08 23:47:31"},
{"to":"ezenwa","Message":"seems good","time":"2015-02-12 17:44:10"},
{"to":"david","Message":"j nman ","time":"2015-02-13 14:43:28"},
{"to":"john","Message":"this is cool","time":"2015-02-13 14:58:28"}
]}

如果我做错了请告诉我..或者如果你有更好的方法请帮助我

最佳答案

如果我没有错误地理解您的要求,您只是想从数据库获取最新消息。

您可以按时间戳排序数据库聊天表中的任何列,按聊天表的时间戳来自数据库的行。

例如

从 chatt 中选择 *,其中 .... 按时间戳 DESC 排序

如果我仍然无法满足您的要求,请告诉我。

关于php - 如何使用 json 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28543187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com