Upravit

Sdílet prostřednictvím


How to: Configure Error and Warning Handling Using the SQLSRV Driver

Download PHP driver

This topic describes how to configure the SQLSRV driver to handle errors and warnings.

By default, the SQLSRV driver treats warnings as errors; a call to a sqlsrv function that generates an error or a warning returns false. To disable this behavior, use the sqlsrv_configure function. When the following line of code is included at the beginning of a script, a sqlsrv function that generates only warnings (no errors) will not return false:

sqlsrv_configure("WarningsReturnAsErrors", 0);

The following line of code resets the default behavior (warnings are treated as errors):

sqlsrv_configure("WarningsReturnAsErrors", 1);

Note

Warnings that correspond to SQLSTATE values 01000, 01001, 01003, and 01S02 are never treated as errors. Regardless of the configuration, a sqlsrv function that generates only warnings that correspond to one of these states will not return false.

The value for WarningsReturnAsErrors can also be set in the php.ini file. For example, this entry in the [sqlsrv] section of the php.ini file turns off the default behavior.

sqlsrv.WarningsReturnAsErrors = 0

For information about retrieving error and warning information, see sqlsrv_errors and How to: Handle Errors and Warnings.

Example

The following code example demonstrates how to disable the default error-handling behavior. The example uses the Transact-SQL PRINT command to generate a warning. For more information about the PRINT command, see PRINT (Transact-SQL).

The example first demonstrates the default error-handling behavior by executing a query that generates a warning. This warning is treated as an error. After changing the error-handling configuration, the same query is executed. The warning is not treated as an error.

The example assumes that SQL Server is installed on the local computer. All output is written to the console when the example is run from the command line.

<?php  
/* Connect to the local server using Windows Authentication. */  
$serverName = "(local)";  
$conn = sqlsrv_connect( $serverName );  
if( $conn === false )  
{  
     echo "Could not connect.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
/* The Transact-SQL PRINT statement can be used to return   
informational or warning messages*/  
$tsql = "PRINT 'The PRINT statement can be used ";  
$tsql .= "to return user-defined warnings.'";  
  
/* Execute the query and print any errors. */  
$stmt1 = sqlsrv_query( $conn, $tsql);  
if($stmt1 === false)  
{  
     echo "By default, warnings are treated as errors:\n";  
     /* Dump errors in the error collection. */  
     print_r(sqlsrv_errors(SQLSRV_ERR_ERRORS));  
}  
  
/* Disable warnings as errors behavior. */  
sqlsrv_configure("WarningsReturnAsErrors", 0);  
  
/* Execute the same query and print any errors. */  
$stmt2 = sqlsrv_query( $conn, $tsql);  
if($stmt2 === false)  
{  
     /* Dump errors in the error collection. */  
     /* Since the warning generated by the query is be treated as   
        an error, this block of code will not be executed. */  
     print_r(sqlsrv_errors(SQLSRV_ERR_ERRORS));  
}  
else  
{  
     echo "After calling ";  
     echo "sqlsrv_configure('WarningsReturnAsErrors', 0), ";  
     echo "warnings are not treated as errors.";  
}  
  
/*Close the connection. */  
sqlsrv_close($conn);  
?>  

See Also

Logging Activity

Programming Guide for the Microsoft Drivers for PHP for SQL Server

SQLSRV Driver API Reference