我确实成功地将基于Web的应用程序从MySQLi转换为PDO,因为我也想使用Microsoft SQL-Server作为数据库。PDO在MySQL数据库中就像一个魅力。现在,我第一次使用MS-SQL进行了尝试,但没有尝试。几乎每个查询都必须更新。这非常令人沮丧。
下面的简单代码使我发疯:
$ComputerGUID = "5BEC3779-B002-46BA-97C4-19158C13001F";
$SqlSelectQuery = "SELECT computermapping.PrinterGUID,
case when computerdefaultprinter.PrinterGUID IS NOT NULL then 1 else 0 end AS isDefaultPrinter
FROM computermapping
LEFT JOIN computerdefaultprinter ON computerdefaultprinter.ComputerGUID = computermapping.ComputerGUID
AND computerdefaultprinter.PrinterGUID = computermapping.PrinterGUID
WHERE computermapping.ComputerGUID = :ComputerGUID";
$SqlStatement = $pdo->prepare( $SqlSelectQuery );
$SqlStatement -> bindValue( ":ComputerGUID", $ComputerGUID, PDO::PARAM_STR );
$SqlStatement->execute();
$SelectQueryNumRows = $SqlStatement->rowCount();
IF ( $SelectQueryNumRows > 0 ) {
$Data = $SqlStatement->fetchAll(PDO::FETCH_ASSOC);
} ELSE {
echo "The query did not return a result ...";
}
它在MySQL上运行正常,并向我返回了结果。
使用Microsoft SQL-Server不会得到结果。(查询未返回结果...)
在Microsoft SQL Server Management Studio中运行相同的查询也可以正常工作:
使用SQL Server Profiler运行代码时监视查询将显示以下内容:
如手册所述:
一些数据库可能返回该语句返回的行数。但是,不能保证所有数据库都具有此行为,并且便携式应用程序不应依赖此行为。
因此,您不应使用rowCount
。但是,当您获取所有数据时,请使用它:
$SqlStatement->execute();
$Data = $SqlStatement->fetchAll(PDO::FETCH_ASSOC);
// in case of no data - empty array will be returned
if ($Data) {
// process $Data;
} else {
echo "The query did not return a result ...";
}
另请注意,如果出现错误,$Data
可能会false
出现这种情况,如果需要,您应该处理此值$Data
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句