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

标签 php mysql

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

相关文章:

php - 服务器更改后 .htaccess 中出现奇怪的问题

PHP-MySQL 标记

iphone - Objective-C 中的在线 MySQL 数据库

mysql - 还需要用 MySQL 5 在 J2EE 6 中编写连接池类吗?

mysql - 无法从 Eclipse 连接到 MySql 数据库 : java/sql/SQLException

mysql - 我可以使用MySQL建立Symfony2 ORM继承关系吗?

javascript - 使用高级自定义字段插件和转发器字段在 Wordpress 中制作幻灯片

php - PHP 脚本中的函数错误

php - 如何在 CentOS 6.x 上更新 PHP 和安装 ICU 库

mysql - 将数据从远程 MySQL 服务器迁移到 Rails App