4
I have this routine below, which used without problems with a Mysql database. However I had to migrate to a SQL Server 2008 database, which was simple.
The only problem is, I don’t know why mine rowCount()
is returning -1
, even with my query working without problem, because if I give a print_r
in the SQL return, all the lines that should come in the query are there.
Follows my code:
public function listar(){
$retorno = array();
$sql = "SELECT m.id, m.descricao, m.link, m.categoria, m.icone FROM menus AS m, grupos AS g, permissoes AS p WHERE (g.id = p.idgrupo AND m.id = p.idmenu) AND (p.status = :pstatus AND g.status = :gstatus AND m.status = :mstatus) AND g.id = :gid ORDER BY m.ordem ;";
$vars = array(":pstatus"=>1,":gstatus"=>1,":mstatus"=>1,":gid"=>$_SESSION['group']);
$stmt = $this->pdo->prepare($sql);
foreach($vars as $index => $value){
$stmt->bindValue($index,$value);
}
if($stmt->execute()){
$count = $stmt->rowCount();
$rows = $stmt->fetchAll(PDO::FETCH_OBJ);
$rows['msg'] = '1';
$rows['length'] = $count;
$i = 0;
while($i < $count){
foreach($rows[$i] as $index => $value){
$rows[$i]->$index = utf8_encode($value);
}
$i++;
}
return $rows;
} else {
return array("msg" => '0');
}
}
Related comment in the PHP manual: http://php.net/manual/en/pdostatement.rowcount.php#113608
– bfavaretto