Bug #69902 [Com]: PDO_DBLIB No Exception or warning thrown on divide by zero statement

From: Date: Tue, 11 Oct 2016 17:57:30 +0000
Subject: Bug #69902 [Com]: PDO_DBLIB No Exception or warning thrown on divide by zero statement
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-204608@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69902&edit=1 ID: 69902 Comment by: adambaratz@php.net Reported by: maxiwheat at gmail dot com Summary: PDO_DBLIB No Exception or warning thrown on divide by zero statement Status: Verified Type: Bug Package: PDO DBlib Operating System: Linux Slackware 14.1 PHP Version: 5.6.10 Assigned To: adambaratz Block user comment: N Private report: N New Comment: 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; Previous Comments: ------------------------------------------------------------------------ [2016-10-11 17:50:51] adambaratz@php.net I misread the result before. There is a result packet, but it's followed by an info packet with a "Division by zero occurred." message. It's being ignored because the severity is 0. According to official docs, it should be 10. See message 3607: https://technet.microsoft.com/en-us/library/cc645601(v=sql.105).aspx Next hypothesis is that these queries run differently over dblib. This query should produce extended error info: BEGIN TRY -- Generate a divide-by-zero error. SELECT 1/0; END TRY BEGIN CATCH SELECT ERROR_SEVERITY() AS ErrorSeverity; END CATCH; GO When run in SSMS, it produces two rowsets: one with no columns, one with the error info: ErrorNumber ErrorSeverity ErrorState ErrorProcedure ErrorLine ErrorMessage 8134 16 1 NULL 2 Divide by zero error encountered. When run in PHP, it produces two rowsets: one with no columns, one with a single NULL result. ------------------------------------------------------------------------ [2016-09-14 21:56:20] adambaratz@php.net I did some testing with MSSQL + FreeTDS (TDS 7.2). The query result includes the "divide by zero" message, but it's labeled as a result (with a NULL value) rather than an error. This is why an exception doesn't bubble up. I'm following up with the FreeTDS team to get a better understanding of why this is and where the fix should be applied. ------------------------------------------------------------------------ [2015-06-22 17:08:32] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=69902&edit=1

« previous php.bugs (#204608) next »