使用MS sql数据库时记录错误


Log error when working with MS sql database

我写了一个php页面/脚本,它连接到ms-sql-db并执行一些插入/更新。可能会出现错误,所以我想将它们记录到一个文件中。

日志记录功能的当前版本为:

function logMsSqlError($fileStream){
        fwrite($fileStream, "Error: ".mssql_get_last_message()."'n");
        fwrite($fileStream,urldecode(http_build_query( error_get_last()))."'n" );
}

它的用法是:

 $res = mssql_query($q, $dbhandle);
        if(!$res) {
                logMsSqlError($fh);
                fclose($fh);
                die("query failed");
        }

问题是,当错误发生时,我在网页上看到了很多有用的信息:

"Warning: mssql_query(): message: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Test1". 
The conflict occurred in database "testDb", table "dbo.testTable", column 'TestColumn'. (severity 16) in /var/www/html/sms/utilities.php on line 31 
Warning: mssql_query(): General SQL Server error: Check messages from the SQL Server (severity 16) in /var/www/html/sms/utilities.php on line 31 
Warning: mssql_query(): Query failed in /var/www/html/sms/utilities.php on line 31"

而在日志文件中,我无法捕捉到所有这些细节。现在我得到:

Error: The statement has been terminated.
type=2&message=mssql_query(): Query failed&file=/var/www/html/sms/utilities.php&line=31

如何在日志文件中获取浏览器错误的详细信息?(网页中的详细信息来自哪里?)

您有两个选项:您可以在php.ini中打开log_errors,也可以使用set_error_handler函数将您自己的日志记录系统与php错误处理程序集成。