|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-06-22 17:08 UTC] maxiwheat at gmail dot com
Description:
------------
When executing queries on Microsoft SQL Server (tested on 2008 and 2014), some queries that should fail and produce an exception (or a warning depending on PDO::ATTR_ERRMODE) just fail silently. This is a problem since we cannot catch or detect and handle them (logging etc.)
I know of "Divide by zero" errors that produce this unexpected behavior, there might be other ones.
However, some other types of SQL errors produce exceptions as expected, there seem to be some specific kind that don't get thrown.
Test script:
---------------
// Make sure all errors are displayed
ini_set('display_errors', '1');
error_reporting(E_ALL);
$dbh = new PDO ("dblib:host=myserver;dbname=MyDB","myUsername","MyPass");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Dividing by zero should throw an exception
$stmt = $dbh->prepare("SELECT 1/0;");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_BOTH);
var_dump($result);
Expected result:
----------------
An exception should be thrown.
Actual result:
--------------
No exception thrown
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 02:00:02 2025 UTC |
Above, the following query was actually used: BEGIN TRY SELECT 1/0; END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage; END CATCH;After some more digging, my conclusion is that pdo_dblib is behaving as it's supposed to. That is, it's handling the results from FreeTDS as they're returned. You have to tweak some session settings to get an error. This can be a little confusing since SSMS has different defaults. <? $db = new PDO(...); $stmt = $db->query(" DECLARE @ANSI_WARN VARCHAR(3) = 'OFF'; IF ( (8 & @@OPTIONS) = 8 ) SET @ANSI_WARN = 'ON'; DECLARE @ARITHABORT VARCHAR(3) = 'OFF'; IF ( (64 & @@OPTIONS) = 64 ) SET @ARITHABORT = 'ON'; DECLARE @ARITHIGNORE VARCHAR(3) = 'OFF'; IF ( (128 & @@OPTIONS) = 128 ) SET @ARITHIGNORE = 'ON'; SELECT @ANSI_WARN AS ANSI_WARNINGS, @ARITHABORT AS ARITHABORT, @ARITHIGNORE AS ARITHIGNORE; "); do { $rowset = $stmt->fetchAll(); } while ($stmt->nextRowset()); var_dump($rowset[0]); // off by default // flip them on $db->exec(' SET ARITHABORT ON SET ARITHIGNORE ON SET ANSI_WARNINGS ON '); $stmt = $db->query(' BEGIN TRY SELECT 1/0; END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage; END CATCH; '); // multiple rowsets will be returned do { $rowset = $stmt->fetchAll(); } while ($stmt->nextRowset()); // the last rowset will contain the error info var_dump($rowset[0]); ?> Note that the TRY/CATCH is needed to trap the error: <? $stmt = $db->prepare('SELECT 1/0'); // the query will succeed echo 'execute: ' . ($stmt->execute() ? 'y' : 'n') . "\n"; // // it will have an empty result set echo "data:\n"; var_dump($stmt->fetchAll()); // with a single column with type int echo 'column count: ' . $stmt->columnCount() . "\n"; // 1 echo "column meta:\n"; var_dump($stmt->getColumnMeta(0)); // computed int // you can get the divide-by-zero error if you know to ask echo "error:\n"; var_dump($stmt->errorInfo()); ?>