带有php循环的pdo mysql只返回1行
更新2-我通过使用子查询实现了这一点,而不是使用$config变量,每个数字都有一个$config变量。我在我的articles\u类别表中添加了一行,使其具有1或0,如果它具有1,则将其包括在内,如下所示:带有php循环的pdo mysql只返回1行,php,mysql,pdo,Php,Mysql,Pdo,更新2-我通过使用子查询实现了这一点,而不是使用$config变量,每个数字都有一个$config变量。我在我的articles\u类别表中添加了一行,使其具有1或0,如果它具有1,则将其包括在内,如下所示: $db->sqlquery("SELECT a . * , c.`category_name` FROM `articles` a LEFT JOIN `articles_categorys` c ON c.`category_id` = a.`category_id` WH
$db->sqlquery("SELECT a . * , c.`category_name`
FROM `articles` a
LEFT JOIN `articles_categorys` c ON c.`category_id` = a.`category_id`
WHERE a.`active` =1
AND a.`category_id`
IN (
(
SELECT `category_id`
FROM `articles_categorys`
WHERE `show_in_rss` =1
)
)
ORDER BY a.`date` DESC
LIMIT ?", array($limit));
更新1-我有一个部分解决方案,我需要第二个方面的帮助(我甚至不知道这是否正确,但这让我发疯)
如果我这样做
$category_array = explode(',', $config['article_rss_categorys']);
$in_sql = '';
$counter = 0;
// count how many there are
foreach($category_array as $cat)
{
if ($counter == 0)
{
$in_sql .= '?';
}
else
{
$in_sql .= ',?';
}
$counter++;
}
我可以把“$in_sql”放在in()中,给我一个人?它是有效的。现在,我需要找到一种方法,将$config['article\u rss\u categoris']中的每个数字输入到查询的第二部分
原创
$db是我的数据库类/连接(所有函数都在此类中)
好的,我的问题是:
$db->sqlquery("
SELECT a.*, c.`category_name`
FROM `articles` a LEFT JOIN `articles_categorys` c
ON c.`category_id` = a.`category_id`
WHERE a.`active` = 1
AND a.`category_id` IN (?)
ORDER BY a.`date`
DESC LIMIT ?", array($config['article_rss_categorys'], $limit)
);
我检查并设置了$config['article\u rss\u categoris']
,其0,1,2,4,6,7
,还设置了$limit
,其15
这是我的查询代码(在$db调用的mysql类中)
我在phpmyadmin中测试了查询,用正确的东西替换了?
,它确实有效,所以数据库很好
然后我试着像这样把结果拿出来公布
while ($line = $db->fetch())
{
// make date human readable
$date = $core->format_date($line['date']);
$output .= "
<item>
<title>{$line['category_name']} > {$line['title']}</title>
<link>http://www.prxa.info/index.php?module=articles_comments&aid={$line['article_id']}</link>
<pubDate>{$date}</pubDate>
<guid>http://www.prxa.info/index.php?module=articles_comments&aid={$line['article_id']}</guid>
</item>";
}
它只返回一行,即最后一行。它应该循环通过它们,最多15个
我不明白为什么它只得到一行?只返回一行,您需要
请参见问题在于它在我的脚本中的其他地方工作,因为它在一个循环中。它不应该继续运行吗?您没有显示调用fetch()的位置,但这是一个常见的错误。您可以显示该代码吗?为什么while循环中除fetch()之外的所有内容都引用$this->STH?您还将使用
return$this->STH->fetch()返回最后一行代码>在最后一个代码段中。这就是你描述的“获取代码”。在你的fetch()函数中做一个var_dump($this->STH->fetchAll()),然后把结果放入你的问题中。这可能是对的。如果是这样,我将删除此答案,他可以发布自己的答案。你是否尝试手动运行查询-只是为了确保它返回多个结果?@alfasin他写道,他在phpmyadmin中测试了它。我不确定你是否可以将占位符绑定到这样的数组。看看这个问题:它不是一个数组,它只是用逗号分隔的数字。但是你绑定了多个值,可能只有最后一个会粘住,我不确定。
while ($line = $db->fetch())
{
// make date human readable
$date = $core->format_date($line['date']);
$output .= "
<item>
<title>{$line['category_name']} > {$line['title']}</title>
<link>http://www.prxa.info/index.php?module=articles_comments&aid={$line['article_id']}</link>
<pubDate>{$date}</pubDate>
<guid>http://www.prxa.info/index.php?module=articles_comments&aid={$line['article_id']}</guid>
</item>";
}
public function fetch()
{
$this->STH->setFetchMode(PDO::FETCH_ASSOC);
return $this->STH->fetch();
}