エラーおよび警告を処理する方法
既定では、SQL Server Driver for PHP は警告をエラーとして扱います。エラーまたは警告を生成する sqlsrv 関数の呼び出しでは、false が返されます。このトピックでは、こうした既定の動作を無効にし、警告をエラーとは別に処理する方法について説明します。
注意
警告をエラーとして扱う既定の動作には、例外がいくつかあります。SQLSTATE 値 01000、01001、01003、および 01S02 に対応する警告は、エラーとして扱われません。
例
次のコード例では、ユーザー定義関数 DisplayErrors および DisplayWarnings を使用して、エラーと警告を処理します。この例は、次の作業を行うことによって警告とエラーを個別に処理する方法を示しています。
- 警告をエラーとして扱う既定の動作を無効にします。
- 従業員の休暇時間数を更新し、残りの休暇時間数を出力パラメータとして返すストアド プロシージャを作成します。従業員の使用可能な休暇時間数が 0 未満の場合、このストアド プロシージャは警告を出力します。
- 従業員ごとにストアド プロシージャを呼び出すことによって複数の従業員の休暇時間数を更新し、発生するすべての警告およびエラーに対応するメッセージを表示します。
- 各従業員の残りの休暇時間数を表示します。
sqlsrv 関数の最初の呼び出し (sqlsrv_configure) で、警告がエラーとして扱われることに注目してください。警告はエラー コレクションに追加されるため、警告をエラーとは別に調べる必要はありません。しかし、sqlsrv 関数の以降の呼び出しでは、警告はエラーとして扱われないため、警告およびエラーを明示的に調べる必要があります。
また、このコード例で、sqlsrv 関数の各呼び出しの後にエラーを調べていることに注目してください。このように、エラーを調べることをお勧めします。
この例では、SQL Server と AdventureWorks データベースがローカル コンピュータにインストールされていることを前提としています。この例をコマンド ラインから実行した場合、すべての出力はコンソールに書き込まれます。この例を AdventureWorks データベースの新規インストールに対して実行すると、3 つの警告と、2 つのエラーが生成されます。最初の 2 つの警告は、データベースへの接続時に生成される標準的な警告です。3 番目の警告が発生するのは、従業員の使用可能な休暇時間数が 0 未満の値に更新されるためです。エラーが発生するのは、従業員の利用可能な休暇時間数が、テーブルの制約に違反して、-40 時間未満の値に更新されるためです。
<?php
/* Turn off the default behavior of treating errors as warnings.
Note: Turning off the default behavior is done here for demonstration
purposes only. If setting the configuration fails, display errors and
exit the script. */
if( sqlsrv_configure("WarningsReturnAsErrors", 0) === false)
{
DisplayErrors();
die;
}
/* 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 the connection fails, display errors and exit the script. */
if( $conn === false )
{
DisplayErrors();
die;
}
/* Display any warnings. */
DisplayWarnings();
/* Drop the stored procedure if it already exists. */
$tsql1 = "IF OBJECT_ID('SubtractVacationHours', 'P') IS NOT NULL
DROP PROCEDURE SubtractVacationHours";
$stmt1 = sqlsrv_query($conn, $tsql1);
/* If the query fails, display errors and exit the script. */
if( $stmt1 === false)
{
DisplayErrors();
die;
}
/* Display any warnings. */
DisplayWarnings();
/* Free the statement resources. */
sqlsrv_free_stmt( $stmt1 );
/* Create the stored procedure. */
$tsql2 = "CREATE PROCEDURE SubtractVacationHours
@EmployeeID int,
@VacationHours smallint OUTPUT
AS
UPDATE HumanResources.Employee
SET VacationHours = VacationHours - @VacationHours
WHERE EmployeeID = @EmployeeID;
SET @VacationHours = (SELECT VacationHours
FROM HumanResources.Employee
WHERE EmployeeID = @EmployeeID);
IF @VacationHours < 0
BEGIN
PRINT 'WARNING: Vacation hours are now less than zero.'
END;";
$stmt2 = sqlsrv_query( $conn, $tsql2 );
/* If the query fails, display errors and exit the script. */
if( $stmt2 === false)
{
DisplayErrors();
die;
}
/* Display any warnings. */
DisplayWarnings();
/* Free the statement resources. */
sqlsrv_free_stmt( $stmt2 );
/* Set up the array that maps employee ID to used vacation hours. */
$emp_hrs = array (7=>4, 8=>5, 9=>8, 11=>50);
/* Initialize variables that will be used as parameters. */
$employeeId = 0;
$vacationHrs = 0;
/* Set up the parameter array. */
$params = array(
array(&$employeeId, SQLSRV_PARAM_IN),
array(&$vacationHrs, SQLSRV_PARAM_INOUT)
);
/* Define and prepare the query to substract used vacation hours. */
$tsql3 = "{call SubtractVacationHours(?, ?)}";
$stmt3 = sqlsrv_prepare($conn, $tsql3, $params);
/* If the statement preparation fails, display errors and exit the script. */
if( $stmt3 === false)
{
DisplayErrors();
die;
}
/* Display any warnings. */
DisplayWarnings();
/* Loop through the employee=>vacation hours array. Update parameter
values before statement execution. */
foreach(array_keys($emp_hrs) as $employeeId)
{
$vacationHrs = $emp_hrs[$employeeId];
/* Execute the query. If it fails, display the errors. */
if( sqlsrv_execute($stmt3) === false)
{
DisplayErrors();
die;
}
/* Display any warnings. */
DisplayWarnings();
/*Move to the next result returned by the stored procedure. */
if( sqlsrv_next_result($stmt3) === false)
{
DisplayErrors();
die;
}
/* Display any warnings. */
DisplayWarnings();
/* Display updated vacation hours. */
echo "EmployeeID $employeeId has $vacationHrs ";
echo "remaining vacation hours.\n";
}
/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt3 );
sqlsrv_close( $conn );
/* ------------- Error Handling Functions --------------*/
function DisplayErrors()
{
$errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
foreach( $errors as $error )
{
echo "Error: ".$error['message']."\n";
}
}
function DisplayWarnings()
{
$warnings = sqlsrv_errors(SQLSRV_ERR_WARNINGS);
if(!is_null($warnings))
{
foreach( $warnings as $warning )
{
echo "Warning: ".$warning['message']."\n";
}
}
}
?>