If I understand right I'm using following stack: PHP <-> PDO <-> MS SQL DBLIB <-> freetds <-> MS SQL Server.
$pdo = new \PDO('dblib:host=192.168.0.10:1433;dbname=MyDb;charset=UTF-8', 'mydb', 'secret', [
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
\PDO::ATTR_STRINGIFY_FETCHES => false,
]);
$statement = $pdo->prepare("EXEC [dbo].[sp_MyStoredProcedure] 1, 2, 3");
if ( ! $statement->execute()) {
throw new \ErrorException('Error executing sp_MyStoredProcedure');
}
$data = $statement->fetchAll(\PDO::FETCH_ASSOC);
I'm setting ERRMODE
to ERRMODE_EXCEPTION
, and there is an error 515 happens in stored procedure, but no exception is thrown, why? $data
is storing empty array and I was considering this as proper result of executing stored procedure. To overcome this issue I've added check $statement->errorInfo()[1]
, through I'm not sure if it proper to check error in this way:
if ( ! $statement->execute() || $statement->errorInfo()[1]) {
throw new \ErrorException('Error executing sp_MyStoredProcedure');
}
Do I'm doing this check properly?
Detailed $statement->errorInfo():
array (
0 => '00000',
1 => 515,
2 => 'General SQL Server error: Check messages from the SQL Server [515] (severity 16) [(null)]',
3 => -1,
4 => 16,
)
Also according to article error 515 is an error of inserting NULL into column which is NOT NULL. But why I don't see this error when executing EXEC [dbo].[sp_MyStoredProcedure] 1, 2, 3
in Microsoft SQL Server Management Studio or tsql (freetds-bin)?