2

在数据库中,我有一个表,其中包含所有主题标签(表主题标签)和主题标签后面的 user_id。在另一个表格中,我保存了带有主题标签(表格照片)描述的帖子。现在,当用户关注该主题标签时,我想选择具有特定主题标签的所有帖子。我怎样才能做到这一点?

4

1 回答 1

1

使用 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();
?>
于 2019-05-08T16:09:57.520 回答