Stev Stev - 5 months ago 48
PHP Question

PDO dblib not catching warnings

I have successfully made my symfony app connect to a MSSQL database using realestateconz/mssql-bundle and Free TDS.

My problem is that when I try to execute a stored procedure, that procedure throws an exception if something goes wrong, but PDO reports nothing back.

If I do the same thing using mssql_* functions I get a warning with the correct error message from MSSQL.

What is PDO doing differently?

Here are the two samples of code;

//PDO Version
try {
$conn = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {

$stmt = $conn->prepare("INSERT INTO importex_parteneri (id_importex, cif_cnp, denumire) VALUES (1, 9671891, 'Nexus Media')");
$result = $stmt->execute();
var_dump($result); //true

$stmt = $conn->prepare("exec dbo.importex_parteneri_exec 1");
$result = $stmt->execute();
var_dump($result); //true

The mssql_* example

$connection = mssql_connect($server, $user , $pass);
mssql_select_db($nexus_bazadate, $connection);

$result = mssql_query("INSERT INTO importex_parteneri (id_importex, cif_cnp, denumire) VALUES (1, 9671891, 'Nexus Media')");

$result = mssql_query("exec dbo.importex_parteneri_exec 1");

* The output is
* bool(true)
PHP Warning: mssql_query(): message: Error 50000, Level 16, State 1, Procedure importex_parteneri_exec, Line 181, Message: PRT012 - Eroare import par9671891 (severity 16) in /home/vagrant/ on line 19
PHP Warning: mssql_query(): General SQL Server error: Check messages from the SQL Server (severity 16) in /home/vagrant/ on line 19


I ended up adding a wraper around PDO::execute function function

execute(\PDOStatement $stmt, array $params = array())
$result = $stmt->execute($params);

$err = $stmt->errorInfo();
switch ($err[0]) {
case '00000':
case '01000':
return true;
//case HY000
return false;


I may be wrong but I don't recall seeing a native way to handle SQL warnings in PDO in a somehow automated way. According to a user comment you can test manually for the SQLSTATE error code and it'll be:

  • '00000' (success)
  • '01000' (success with warning)

If it actually works, it's of course annoying to do by hand. If you have a custom layer on top of PDO you can always do it in your custom exec method.