本文介绍了PDO :: rowCount()返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面具有此功能.我曾经在 MySQL 数据库上运行.我不得不转向一个 SQL Server 2008 ,这很容易.

i have this function below. that i used to run on a MySQL DB. I had to move to a SQL Server 2008, easy thing.

但是,此后,rowCount()一直返回-1,我以前从未发生过这种情况.我确定我的SQL查询返回了结果,因为如果我在返回值上执行print_r()($rows var),则所有内容都会被打印出来.

But, after that, the rowCount() keeps returning -1, I've never had this happen before. I'm sure that my SQL query is returning the results, because if I do a print_r() on my return (the $rows var), everything gets printed.

因此,如果有人遇到此问题,请帮助我解决这个问题.

So, if anyone had this issue, please help me to figure this out.

很抱歉出现语法错误.

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');
    }

}

推荐答案

帅哥.

prepare()语句应该再接收一个参数. array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL).

The prepare() statement should receive one more parameter. array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL) .

$ stmt = $ this-> pdo-> prepare($ sql,array(PDO :: ATTR_CURSOR => PDO :: CURSOR_SCROLL));

$stmt = $this->pdo->prepare($sql,array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));

然后rowCount()应该可以正常工作.

After that the rowCount() should work fine.

感谢答案@Rasclatt.

Thanks for the answer @Rasclatt.

这篇关于PDO :: rowCount()返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 09:46