Retrieving Error Messages

suggest change

When a query goes wrong, it is important to fetch the error message(s) returned by the driver to identify the cause of the problem. The syntax is:

sqlsrv_errors([int $errorsOrWarnings]);

This returns an array with:

|Key|Description| |—|—| |SQLSTATE| The state that the SQL Server / OBDC Driver is in| |code|The SQL Server error code| |message|The description of the error|

It is common to use the above function like so:

$brokenQuery = "SELECT BadColumnName FROM Table_1";
$stmt = sqlsrv_query($conn, $brokenQuery);

if ($stmt === false) {
    if (($errors = sqlsrv_errors()) != null) {
        foreach ($errors as $error) {
            echo "SQLSTATE: ".$error['SQLSTATE']."<br />";
            echo "code: ".$error['code']."<br />";
            echo "message: ".$error['message']."<br />";
        }
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Using SQLSRV:
* Retrieving Error Messages

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
67 Using SQLSRV
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR