php - 使用数组 PHP 获取多个帖子

标签 php mysql sql

所以这是我的问题。我正在查看用户订阅的位置,结果如下所示

Array ( [0] => 11 [1] => 111 )

所以基本上它们被替换为用户 11 和用户 111,所以现在我想获得用户 11 111 个帖子。这基本上就是我被困的地方。这是我用来获取该数组的代码

$email = $call['email'];

 //Get user info
$ginfo = $con->prepare("SELECT * FROM users WHERE email = :email");
$ginfo->bindValue(':email', $email);
$ginfo->execute();

$id = $ginfo->fetch(PDO::FETCH_ASSOC);
$id = $id['id'];

//Get user subs
$gsub = $con->prepare("SELECT * FROM subscriptions WHERE user_id = :id");
$gsub->bindValue(':id', $id, PDO::PARAM_INT);
$gsub->execute();

$sub_to = $gsub->fetchAll(PDO::FETCH_COLUMN, 2);

print_r($sub_to);

任何帮助都会非常棒。我在这上面花了好几个小时,但我还是个新手。谢谢

我的订阅表

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| user_id     | int(11)          | YES  |     | NULL    |                |
| sub_to      | int(11)          | YES  |     | NULL    |                |
| date_subbed | varchar(255)     | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+

还有我的文章表

+----------------+------------------+------+-----+---------+----------------+
| Field          | Type             | Null | Key | Default | Extra          |
+----------------+------------------+------+-----+---------+----------------+
| id             | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| from           | varchar(255)     | YES  |     | NULL    |                |
| date_published | datetime         | YES  |     | NULL    |                |
| content        | text             | YES  |     | NULL    |                |
| title          | varchar(255)     | YES  |     | NULL    |                |
| from_id        | int(11)          | YES  |     | NULL    |                |
+----------------+------------------+------+-----+---------+----------------+

最佳答案

最好将此作为单个查询完成,或者通过发出 INNER JOINarticle, subscription 之间,或使用 IN()子查询。

article.from_id引用文献 subscriptions.sub_to ,那将是您的加入专栏。

此版本将使用 :id您已经从之前的查询中收到。

SELECT
  article.*
FROM
  article
  INNER JOIN subscriptions ON article.from_id = subscriptions.sub_to
WHERE
  subscriptions.user_id = :id

或者,如果您愿意,可以使用 IN() 来完成子查询而不是连接

SELECT
  article.*
FROM
  article
WHERE from_id IN (SELECT sub_to FROM subscriptions WHERE user_id = :id)

更好的版本:

但是,您可以使用附加连接消除第一个查询:

SELECT
  article.*
FROM
  article
  INNER JOIN subscriptions ON article.from_id = subscriptions.sub_to
  INNER JOIN users ON subscriptions.user_id = users.id
WHERE
  users.email = :email

然后绑定(bind):email并执行:

$articles = $con->prepare(....);
$articles->bindValue(':email', $email);
$articles->execute();
$rows = $articles->fetchAll();
// All articles are now in $rows
print_r($rows);

可能更好:

预计需要文章的海报详情:如果您还需要文章作者的用户详情,请再次反对users .

SELECT
  article.*,
  /* user details of the article authors */
  sub_authors.*
FROM
  article
  INNER JOIN subscriptions ON article.from_id = subscriptions.sub_to
  /* First join against users is to identify the subscriber by email */
  INNER JOIN users ON subscriptions.user_id = users.id
  /* additional join to get subscribed author user info */
  INNER JOIN users AS sub_authors ON subscriptions.sub_to = sub_authors.id
WHERE
  users.email = :email

绑定(bind)并执行 :email和以前一样,现在所有文章和作者的详细信息都会导致 $rows .

关于 SELECT article.*, sub_authors.* 的最后说明以上 - 实际上我不会使用 *而是明确列出列。这很重要,因为两个表都有一个 id获取行时无法正确区分的列。最好用别名列出它们,例如:

SELECT
  article.id AS article_id,
  article.title,
  article.content,
  ...,
  sub_authors.id AS author_id,
  sub_authors.name,
  ...

关于php - 使用数组 PHP 获取多个帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25710844/

相关文章:

php - 如何使用 PHP 计算天数中两个字符串日期的差异?

php - 将 Javascript 变量显示为 PHP 变量

php - 使用 codeigniter 传递下拉列表的字符串值

php - 我怎样才能解决这个简单的 php MySQL 搜索查询

mysql - 即使卸载 wamp 服务器后也无法删除 wamp 文件夹

sql - 选择后提交

使用case时mysql查询不返回所有结果

php - 如何从 PHP 调用 R 脚本?

mysql - SQL:搜索/替换,但仅在值第一次出现在记录中时

sql - postgres - SELECT/GROUP BY 中的部分列 - 列必须出现在 GROUP BY 子句中或用于聚合函数