问题描述
当我在 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;
}
var_dump($result->fetch()) 在 MySQL 和 SQL Server 中
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"
}
var_dump($numrows) 在 SQL Server 中
var_dump($numrows) in SQL Server
int(-1)
var_dump($numrows) 在 MySQL 中
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!