Reputation: 2087
UPDATE 2 - I managed to do it by using a subquery, rather than having a $config var with each number i added a row to my articles_category table to have 1 or 0, if it has 1 include it like so:
$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));
UPDATE1 - I have a partial solution i need help with the second bit (i don't even know if this is going the right way about it but it's driving me nuts).
If i do this;
$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++;
}
I can put "$in_sql" inside the IN () to give me the individual ? and it works. Now i need to find a way to get each number from inside $config['article_rss_categorys'] into the second part of the query?
ORIGINAL;
$db is my database class/connection (all the functions are in this class).
Okay so here is my query:
$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)
);
I check and the $config['article_rss_categorys']
is set and its 0,1,2,4,6,7
, also $limit
is set and it's 15
.
Here is my query code (inside the mysql class called by $db);
try
{
$this->STH = $this->database->prepare($sql);
foreach($objects as $k=>$p)
{
// +1 is needed as arrays start at 0 where as ? placeholders start at 1 in PDO
if(is_numeric($p))
{
$this->STH->bindValue($k+1, (int)$p, PDO::PARAM_INT);
}
else
{
$this->STH->bindValue($k+1, $p, PDO::PARAM_STR);
}
}
return $this->STH->execute();
$this->counter++;
}
catch (PDOException $e)
{
$core->message($e->getMessage());
}
I tested the query in phpmyadmin replacing ?
with the correct stuff and it does work so the database is fine.
I then try to fetch and put out the results like so;
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>";
}
This is my fetch code (inside the mysql class called by $db);
public function fetch()
{
$this->STH->setFetchMode(PDO::FETCH_ASSOC);
return $this->STH->fetch();
}
It only returns one row, the last one. It is supposed to be looping through them all up to a max of 15.
I don't get why it's only getting one?
Upvotes: 0
Views: 992
Reputation: 18440
PDOStatement::fetch() only returns one row, you need PDOStatement::fetchAll().
Upvotes: 1