Bug #74613 [Opn->Nab]: PDO Exceptions not thrown when OUTPUT clause used in a failed query

From: Date: Thu, 29 Jun 2017 10:26:42 +0000
Subject: Bug #74613 [Opn->Nab]: PDO Exceptions not thrown when OUTPUT clause used in a failed query
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-209729@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74613&edit=1 ID: 74613 Updated by: adambaratz@php.net Reported by: dan dot marra at gmail dot com Summary: PDO Exceptions not thrown when OUTPUT clause used in a failed query -Status: Open +Status: Not a bug Type: Bug -Package: PDO related +Package: PDO DBlib Operating System: Linux PHP Version: Irrelevant Block user comment: N Private report: N New Comment: I wouldn't describe this as an issue with pdo_dblib. The OUTPUT clause does change how the query operates. If you're using FreeTDS, you can enable logging and compare the server responses: http://www.freetds.org/userguide/logging.htm When you use the OUTPUT clause, the constraint issue is returned, but as a message instead of an error. pdo_dblib is only doing what it's told. My preference is that it doesn't apply special logic on top of what the DB is returning. What works for you and your particular setup may not be appropriate for everyone. You can catch this state by checking for a return value of false from $stmt->fetchColumn. You can then call $stmt->errorInfo() if you want to do something with the returned message. Previous Comments: ------------------------------------------------------------------------ [2017-05-18 15:00:29] dan dot marra at gmail dot com Description: ------------ --- From manual page: http://www.php.net/language.control-structures --- When you have an error in a t-sql query, but that query ALSO has an OUTPUT clause in it, PDO does not throw an exception when expected (likely because there is a return value). Test script: --------------- /* in MSSQL */ CREATE TABLE foobar ( foo int not null ); ALTER TABLE foobar ADD CONSTRAINT unique_foo UNIQUE (foo); GO insert into foobar (foo) values (1); go /* IN PHP */ <?php $conn = new PDO('dblib:host='.$hostname.';dbname='.$database, $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare("insert into foobar (foo) output INSERTED.foo values (1)"); try { $stmt->execute(); $foo = $stmt->fetchColumn(); } catch (PDOException $ex) { // THIS DOES NOT GET HIT, But it *should* because a unique constraint is violated. die("you dun goofed son"); } echo "all is good"; ?> Expected result: ---------------- what SHOULD happen is that an exception should be thrown due to the unique constraint violation. Instead, it is ignored. If you remove the output clause from the query, this works as expected. Actual result: -------------- Error from mssql is ignored ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=74613&edit=1

« previous php.bugs (#209729) next »