Ando
Ando

Reputation: 11439

Log error when working with MS sql database

I wrote a php page/script that connects to an ms sql db and execute some inserts/updates. It is possible that errors might occur so I would like to log them to a file.

The current version of the logging function is:

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

And it's used like:

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

The problem is that in the web page I see a lot of useful information when an error occurs:

"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"

while in the log file I'm unable to catch all these details. Right now I get:

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

How can I get the details I have in the browser error in the log file? (where do the details in the web page come from?)

Upvotes: 0

Views: 1517

Answers (1)

Lusitanian
Lusitanian

Reputation: 11132

You have a couple options: you can either turn on log_errors in php.ini or use the set_error_handler function to integrate your own logging system with the PHP error handler.

Upvotes: 1

Related Questions