gpt4 book ai didi

php - 如何选择用户在 SQL 和 PHP 中关注的具有特定主题标签的所有帖子?

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

在数据库中,我有一个表,其中包含所有主题标签 ( Table hashtag ) 和主题标签后面的 user_id。在另一个表中,我保存了带有主题标签(Table photo)的描述的帖子。现在,我想在用户关注该主题标签时选择所有具有特定主题标签的帖子。我该怎么做?

最佳答案

使用sql,

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$mysqli = new mysqli("localhost", "username", "password", "databaseName")//replacing username,password,databasename with the appropriate values;
$mysqli->set_charset("utf8mb4");
} catch(Exception $e) {
error_log($e->getMessage());
exit('Error connecting to database'); //Should be a message a typical user could understand
}

$stmt = $mysqli->prepare("SELECT * FROM posts WHERE hashtag = ?");
$stmt->bind_param("s", $_POST['hashtag']);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows === 0) exit('No rows');
while($row = $result->fetch_assoc()) {
$posts[] = $row['posts'];//or whatever the row of the posts body is

}

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
$row["posts"]. " " . $row["hashtag"]. "<br>";
}
}
else{
echo 'NO RESULTS FOUND!!';
}

$stmt->close();
?>

关于php - 如何选择用户在 SQL 和 PHP 中关注的具有特定主题标签的所有帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56044522/

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