在数据库中,我有一个表,其中包含所有主题标签 ( 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/