PDO rowCount() works on MySQL but not in SQL Server 2008 R2(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;
}
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PDO rowCount() 适用于 MySQL,但不适用于 SQL Server 2008 R2
基础教程推荐
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
