I can’t figure out the error above. Has anyone seen this before. I am using PHP with Smarty and incorporating PDO into my code.
PHP var_dump of code $data $smarty->assign("results", $data);
object(PDOStatement)#22 (1) {
["queryString"]=> string(236) "SELECT p.id,p.brag,p.brag_desc,p.brag_id,p.user_id, p.panel_id,p.domainurl,p.type,p.price FROM ruj_users_bg as p left join ruj_users as u on p.user_id=u.id WHERE u.status !=0 and 1 and p.status = 1 GROUP BY p.id DESC LIMIT 0 OFFSET 25" }
PHP Error from Smarty template:
Notice: Object of class PDOStatement could not be converted to int in /var/www/vhosts/somesite.com/httpdocs/templates_c/3d14690fd8419657273a6dce45bbca85dfc1e261.file.bragsdata.tpl.php on line 25
Line 25 from Smarty template:
$_smarty_tpl->tpl_vars[‘smarty’]->value[‘section’][‘res’][‘loop’] = is_array($_loop=$_smarty_tpl->getVariable(‘results’)->value) ? count($_loop) : max(0, (int)$_loop); unset($_loop);
Not sure where the problem is here. Your help is very appreciated.
update: Function call with fetchAll(PDO::FETCH_ASSOC);
$data = fetchData($limit_start,$page_records);
function fetchData$limit_start,$page_records){
$db = Core::getInstance();
$sql = "SELECT id FROM ruj_users_bg WHERE
(type = :type3 OR type = :type4)
AND status =:uno
ORDER BY id DESC LIMIT :lim OFFSET :page";
$res = $db->dbh->prepare($sql);
$res->bindValue(':type3',3, PDO::PARAM_INT);
$res->bindValue(':type4',4, PDO::PARAM_INT);
$res->bindValue(':uno',1, PDO::PARAM_INT);
$res->bindValue(':lim',(int)$limit_start, PDO::PARAM_INT);
$res->bindValue(':page',(int)$page_records, PDO::PARAM_INT);
$res->execute();
$res->fetchAll(PDO::FETCH_ASSOC);
return $res;
}
That’s because
PDOStatementis strictly not an array type. It implementsTraversablethough, so I’m surprised that Smarty doesn’t accept it as something that can be looped over.To turn your database results into a standard array, you’d have to use
->fetchAll():