note 71067 deleted from function.mssql-get-last-message by bobby
| From: | bobby@php.net | Date: | Mon, 13 Nov 2006 16:16:46 +0000 |
| Subject: | note 71067 deleted from function.mssql-get-last-message by bobby | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-119671@lists.php.net to get a copy of this message | ||
Note Submitter: php at cybertinus dot nl
----
I faced the same problem as Klaus. mssql_get_last_message only returns the last line of an error. I
tried Klaus' script, but that didn't work for me, because I use Microsoft SQL Server 2005
and not 2000, so @@ERROR is depricated and stuff. Also, a lot of messages in the error staid at %l,
%s and %s. And an error
"Error %i, on line %i. Statement: %s" isn't that usefull :). So I becan searching the
internet for a possible solution. I found the website http://www.4guysfromrolla.com/webtech/041906-1.shtml.
With that article I was able to write the following function:
<?php
function query($query, $db_conn, $message,)
{
$sql = "BEGIN TRY\n";
$sql .= "\t".$query."\n";
$sql .= 'END TRY
BEGIN CATCH
SELECT \'Error: \' + ERROR_MESSAGE()
END CATCH';
$result = @mssql_query($sql, $db_conn);
$return = mssql_fetch_assoc($result);
if(isset($return['computed']))
{
echo $sMelding, '. MSSQL returned:
'.$aReturn['computed'].'.<br>Executed query: '.nl2br($sql);
return false;
}
else
{
return $result;
}
}
?>
If you use this function, then you have to make your scripts as follows:
<?php
//make a connection to an MSSQL Server and save that connection in $db_conn
if(query('SELECT * FROM table', $db_conn, 'The selection failed'))
{
// The query was succesfull completed
}
else
{
// There was some error, and the error is already displayed on your screen, so this else-part is
usually not nessary.
}
?>
I hope this wil help somebody.
Best regards,
Cybertinus