แก้ไข

แชร์ผ่าน


sqlsrv_errors

Download PHP driver

Returns extended error and/or warning information about the last sqlsrv operation performed.

The sqlsrv_errors function can return error and/or warning information by calling it with one of the parameter values specified in the Parameters section below.

By default, warnings generated on a call to any sqlsrv function are treated as errors; if a warning occurs on a call to a sqlsrv function, the function returns false. However, warnings that correspond to SQLSTATE values 01000, 01001, 01003, and 01S02 are never treated as errors.

The following line of code turns off the behavior mentioned above; a warning generated by a call to a sqlsrv function does not cause the function to return false:

sqlsrv_configure("WarningsReturnAsErrors", 0);  

The following line of code reinstates the default behavior; warnings (with exceptions, noted above) are treated as errors:

sqlsrv_configure("WarningsReturnAsErrors", 1);  

Regardless of the setting, warnings can only be retrieved by calling sqlsrv_errors with either the SQLSRV_ERR_ALL or SQLSRV_ERR_WARNINGS parameter value (see Parameters section below for details).

Syntax

  
sqlsrv_errors( [int $errorsAndOrWarnings] )  

Parameters

$errorsAndOrWarnings[OPTIONAL]: A predefined constant. This parameter can take one of the values listed in the following table:

Value Description
SQLSRV_ERR_ALL Errors and warnings generated on the last sqlsrv function call are returned.
SQLSRV_ERR_ERRORS Errors generated on the last sqlsrv function call are returned.
SQLSRV_ERR_WARNINGS Warnings generated on the last sqlsrv function call are returned.

If no parameter value is supplied, both errors and warnings generated by the last sqlsrv function call are returned.

Return Value

An array of arrays, or null. Each array in the returned array contains three key-value pairs. The following table lists each key and its description:

Key Description
SQLSTATE For errors that originate from the ODBC driver, the SQLSTATE returned by ODBC. For information about SQLSTATE values for ODBC, see ODBC Error Codes.

For errors that originate from the Microsoft Drivers for PHP for SQL Server, a SQLSTATE of IMSSP.

For warnings that originate from the Microsoft Drivers for PHP for SQL Server, a SQLSTATE of 01SSP.
code For errors that originate from SQL Server, the native SQL Server error code.

For errors that originate from the ODBC driver, the error code returned by ODBC.

For errors that originate from the Microsoft Drivers for PHP for SQL Server, the Microsoft Drivers for PHP for SQL Server error code. For more information, see Handling Errors and Warnings.
message A description of the error.

The array values can also be accessed with numeric keys 0, 1, and 2. If no errors or warnings occur, null is returned.

Example

The following example displays errors that occur during a failed statement execution. (The statement fails because InvalidColumName is not a valid column name in the specified table.) The example assumes that SQL Server and the AdventureWorks database are 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 and   
specify the AdventureWorks database as the database in use. */  
$serverName = "(local)";  
$connectionInfo = array( "Database"=>"AdventureWorks");  
$conn = sqlsrv_connect( $serverName, $connectionInfo);  
if( $conn === false )  
{  
     echo "Could not connect.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
/* Set up a query to select an invalid column name. */  
$tsql = "SELECT InvalidColumnName FROM Sales.SalesOrderDetail";  
  
/* Attempt execution. */  
/* Execution will fail because of the invalid column name. */  
$stmt = sqlsrv_query( $conn, $tsql);  
if( $stmt === false )  
{  
      if( ($errors = sqlsrv_errors() ) != null)  
      {  
         foreach( $errors as $error)  
         {  
            echo "SQLSTATE: ".$error[ 'SQLSTATE']."\n";  
            echo "code: ".$error[ 'code']."\n";  
            echo "message: ".$error[ 'message']."\n";  
         }  
      }  
}  
  
/* Free connection resources */  
sqlsrv_close( $conn);  
?>  

See Also

SQLSRV Driver API Reference

About Code Examples in the Documentation