PDO rowCount()在MySQL上有效,但在SQL Server 2008 R2中不可用 [英] PDO rowCount() works on MySQL but not in SQL Server 2008 R2

查看:79
本文介绍了PDO rowCount()在MySQL上有效,但在SQL Server 2008 R2中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在SQL Server 2008中获得行数时遇到问题,因为我的代码可以使用MySQL很好地工作,但不能在SQL Server中.

I have a problem when I get number of rows in SQL Server 2008 because my code works fine using MySQL but not in SQL Server.

$sql = "SELECT TOP 1 U.Id , U.Name, U.Profile,  P.Name NameProfile
        FROM sa_users U
        INNER JOIN sa_profiles P ON P.Id = U.Profile
        WHERE User = :user  AND Pass = :pass";

$result = $this->dbConnect->prepare($sql) or die ($sql);
$result->bindParam(':user',$this->data['username'],PDO::PARAM_STR);
$result->bindParam(':pass',$this->data['password'],PDO::PARAM_STR);

if (!$result->execute()) {
    return false;
}

$numrows = $result->rowCount();
$jsonLogin = array();

var_dump($numrows);

if($numrows > 0) {
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        $jsonLogin = array( 
            'name' => $row['Name'],
            'id' => $row['Id'],
            'profile' => $row['Profile'],
            'n_profile' => $row['NameProfile']
        );
    }

    $jsonLogin['area'] = 'another';
    return $jsonLogin;
} else {
    return false;
}

在MySQL和SQL Server中

var_dump($ result-> fetch())

var_dump($result->fetch()) in MySQL and SQL Server

array(8) {
["Id"]=>
string(1) "1"
[0]=>
string(1) "1"
["Nombre"]=>
string(13) "Administrador"
[1]=>
string(13) "Administrador"
["Perfil"]=>
string(1) "1"
[2]=>
string(1) "1"
["NomPerfil"]=>
string(13) "Administrador"
[3]=>
string(13) "Administrador"
}

在SQL Server中

var_dump($ numrows)

var_dump($numrows) in SQL Server

int(-1)

在MySQL中

var_dump($ numrows)

int(1)

致谢.

推荐答案

只需引用手册:

如果关联的PDOStatement执行的最后一条SQL语句是 SELECT语句,某些数据库可能返回行数 该语句返回的值.但是,不能保证此行为 用于所有数据库,并且不应该依赖用于便携式数据库 应用程序.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

这篇关于PDO rowCount()在MySQL上有效,但在SQL Server 2008 R2中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆