13
votes

I am connecting to an SQL Server database (SQL Server 2008 R2 SP2 x64) from a Linux server running PHP 5.3.19 using the following lines:

$this->dbLink = new PDO(
    'dblib:host='.$this->host.';dbname='.$this->database,
    $this->user,
    $this->password,
    array(
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    )
);

The connection works perfectly fine, except that ATTR_ERRMODE works as ERRMODE_WARNING, no matter what I do.

I have tried setting it using $this->dbLink->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right before the query call, but it has no effect.

If I use getAttribute(), I can confirm that PDO::ATTR_ERRMODE is indeed set to int(2) (ERRMODE_EXCEPTION), but I still get warnings and PHP keeps chugging along with obviously wrong queries.

1
Additional data: FreeTDS is used (according to phpinfo()). In my freeTDS config (/etc/freetds.conf), I found that 4.2 is the default version. It's compatible, but awfully limited compared to 7.2, which is used by SQL Server 2005 and 2008. I'll see with the sysadmin if this configuration can be changed.nicbou
I hope you are using proper try{}catch(){} method to handle the exceptions, otherwise I can't tell what the problem might be. What do you get when you pass invalid query, what does the error say?samayo
No exceptions are thrown at all. If I pass an invalid query, it will keep going with a warning. I temporarily resolved to a simply sanity check (if query===false), but otherwise PHP kept chugging along.nicbou
I know that my query is wrong. I have deliberately used a wrong query to test exception handling. My problem is that I get warning instead of exceptions. There is no extra code, just a standard call to prepare() and execute(). No matter what I call, I should be getting exceptions, not warnings.nicbou
I suppose it's a bug, in which case you should open a new report, because that one seems to be outdatted.samayo

1 Answers

-1
votes

The PDO_DBLIB is an experimental extension and should not be used for production environments.

http://www.php.net/manual/en/ref.pdo-dblib.php

If you can, you should use PDO_SQLSRV instead:

http://www.php.net/manual/en/ref.pdo-sqlsrv.php

Just change your connection setting from dblib: to the PDO_SQLSRV driver dns prefix sqlsrv:

http://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php

Let me know if that works for you