mssql_get_last_message

(PHP 3, PHP 4, PHP 5)

mssql_get_last_message --  Returns the last message from the server

Description

string mssql_get_last_message ( void )

警告

本函数暂无文档,仅有参数列表。


add a note add a note User Contributed Notes
rodflash at rodflash dot com
23-Feb-2006 08:29
If you want to view the last ID inserted, mssql_get_last_message() will not return this.
MySQL have a function mysql_insert_id() that returns it. MSSQl don't.
To resolve this, you must run this query:

$query="SELECT @@IDENTITY as last_insert_id"
mssql_query($query, $connection);

and it will return the last ID inserted in the database.
NOjewlfSPAM at NOattSPAM dot net
29-Jul-2005 10:48
As Klaus pointed out earlier, the command MSSQL_GET_LAST_MESSAGE() only returns the last line of an error message and that may not be enough for debugging.

I had a failing MSSQL_QUERY returning "the statement has been terminated" via MSSQL_GET_LAST_MESSAGE() so I tried Klaus' technique to get more information.

Unfortunately, the connection to the database was also being broken, keeping Klaus' technique from looking up the error number.

The solution to my case was to increase the PHP timeout for MSSQL queries from the default 60 seconds to 300 by adding this to PHP.INI:

mssql.timeout = 300
jessicax at mymelody dot com
12-Jul-2005 11:44
I've noticed that there's a few people putting really elongated code out there for a MSSQL Connection / Error function.

Firstly, i think that rewriting pre-existing functions is a waste of parsing time, so if you're going to try and return an error, usee mssql_get_last_message() as you would with mysql_error().

Here is an adapted version of a MySQL Connection function;

   function mssql_autoconnect() {
       $cString = parse_url($config['connect_url']);
       $cLink = mssql_connect($cString['host'], $cString['user'], $cString['pass']) or die(mssql_get_last_message());
       mssql_select_db(preg_replace("///",$cString['path']),$cLink);
       return $cLink;
   }
klaus dot kuehne at gga-hannover dot de
06-Jun-2005 07:27
MSSQL_GET_LAST_MESSAGE() has  the major disadvantage, that only the last row of the SQLServer message will be returned. And in most cases, this last row has no significant information or is even empty.

In the meanwhile, I migrated my PHP-SQLServer application from MSSQL to ODBC, and now I use ODBC_ERRORMSG() instead of MSSQL_GET_LAST_MESSAGE(). And this function works o.k.

I think that's a good idea to use ODBC instead of the native database interfaces. The performance seems to be at the same level. And your application can easily ported to other database systems.

The only minus of ODBC vs. MSSQL known to me is that ODBC cannot process return values of stored procedures. No problem for most developers!

Regards,

Klaus

P.S.: Here is a small workaround for, the malfunctioning of MSSQL_GET_LAST_MESSAGE:

   function get_sql_error ($con) {
  
       $sql    = "select @@ERROR as code";
       $result = mssql_query($sql, $con);
       $row    = mssql_fetch_array($result);
       $code  = $row["code"]; // error code
       $sql    = "select cast (description as varchar(255)) as errtxt from master.dbo.sysmessages where error = $code and msglangid = 1031"; // german
       $result = mssql_query($sql, $con);
       $row    = mssql_fetch_array($result);
       if ($row)
           $text  = $row["errtxt"]; // error text (with placeholders)
       else
           $text  = "onknown error";
       mssql_free_result($result);
       return "[$code] $text";
   }
drunkennewfiemidget
23-Mar-2005 04:40
I've found mssql_get_last_message to be useful for fetching errors in the event MSSQL queries fail.

<?php

$q
= @mssql_query("asdfasdfasfdasfd");
if (!
$q)
     print
"MSSQL Query failed: " . mssql_get_last_message() . "<br />\n";

?>
ripfel at thinktank dot de
25-May-2002 01:58
If you work with stored procedures and want to have a neat error-handling, try this:
procedure:
CREATE PROCEDURE TEST_ERROR  AS
RAISERROR('myMessage:test', 2, 1) WITH SETERROR

2 is error-severity, which should be below 11 to prevent php to output error directly.
you get this message with mssql_get_last_message()
after executing the stored procedure.
To prevent your code from reacting on all messages you can define a string (e.g. 'myMessage:') and parse for it:
<?php
$db
= mssql_connect(DBHost, DBLogin, DBPassword);
mssql_select_db(DBName, $db);
$query = "exec TEST_ERROR";
$rs = mssql_query($query, $db);
$lastMsg = mssql_get_last_message();
if
strstr($lastMsg, 'myMessage:') echo $lastMsg;
?>
php-contrib at i-ps dot nospam dot net
28-Jan-2002 02:24
With ref to last_insert_id;

you can also do "SELECT ident_current('table_name')" with msSQL, which is the same thing.
Don't forget that this counts as a seperate SQL query, so you will have to fetch the results as well, which isn't as neat as MySQL_insert_id().
richard at alpinenetworking dot com
30-Jul-2001 05:31
To get a numeric error code from mssql you can do a select that looks something like "select @@ERROR as ErrorCode".

@@ERROR is a global variable that always contains the error code for the last SQL statement run on the current connection.  If there is no error, code will equal 0.

@@IDENTITY is another useful one to know.  It is the value of the last identity created (similar to MySQL's auto_increment field) and with this you can create a function that works like MySQL's mysql_insert_id() function.
07-Apr-2001 12:48
MS SQL doesn't set errors as mysql does. $php_error is set as a string that doesn't help at all. The error comes in form of a Warning, which isn't pretty. So what you can do is capture the output of the warning and create your own error message, something like this:

function treat_mssql_error($buffer)
   {
       $buffer=ereg_replace("<br>\n<b>Warning</b>:  MS SQL message:","<b>Error in query (SQL Server)</b>: ",$buffer);
       $buffer=explode("(severity",$buffer);
       return $buffer[0]."</font>";
   }
  
   function my_query($query,$cnx)
   {
       global $sql_error;
       ob_start();
       $result=mssql_query($query,$cnx);
       if(!$result)
       {
           $sql_error=treat_mssql_error(ob_get_contents());
           ob_end_clean();
           return false;
       }
       ob_end_clean();
       return true;
   }

you can set treat_mssql_error() to fit your needs, and there u go. A personal error handler.