I have this routine below, which I 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 that, I do not know why my rowCount()
is returning -1
, even with my query working without problem, because if I give print_r
in SQL return, all the lines that should come in the query are there.
Follow 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');
}
}