ITPUB??ì3
ITPUB论坛 » WEB程序开发 » how to realize mssql_error which is equalent to mysql_error in php


标题: how to realize mssql_error which is equalent to mysql_error in php
离线 liyihongcug
高级会员



精华贴数 1
个人空间 0
技术积分 9573 (122)
社区积分 1043 (893)
注册日期 2004-7-15
论坛徽章:8
会员2007贡献徽章铁扇公主授权会员2008年新春纪念徽章开发板块每日发贴之星数据库板块每日发贴之星
开发板块每日发贴之星ITPUB新首页上线纪念徽章    

发表于 2008-1-23 13:59 
how to realize mssql_error which is equalent to mysql_error in php

mssql_get_last_message
(PHP 4, PHP 5, PECL odbtp:1.1.1-1.1.4)

mssql_get_last_message — Returns the last message from the server

Description
string mssql_get_last_message ( void )
Warning
This function is currently not documented; only the argument list is available.




mssql_guid_string mssql_free_statement
--------------------------------------------------------------------------------
Last updated: Sun, 25 Nov 2007
  
add a note User Contributed Notes
mssql_get_last_message
deyura at gmail dot com
26-Apr-2007 06:49
another exsample

mssql_query("INSERT INTO catalog (id, temp) VALUES (1, 'temp')" or exit(mssql_get_last_message());
user at php dot com
11-Feb-2007 06:38
To cybertinus:

I think BEGIN/END TRY only works in MSSQL 2005.
php at cybertinus dot nl
13-Nov-2006 03:50
Here I am again . My last function stinks (well, actually, MSSQL doesn't always does the same thing with different errors :/. That last function can't cope with that. The following function can cope with that. The only weird thing I had with it was that when I entered a table, which don't exists, in my SELECT query, the first mssql_get_last_message() doesn't always gets the correct message. It sometimes gets the message from the last action. I have no explination for it. Anyway: this is the beter version of my last function.  The usage is explained in my last post:

<?php
function query($sQuery, $hDb_conn, $sError, $bDebug)
{
    if(!$rQuery = @mssql_query($sQuery, $hDb_conn))
    {
        $sMssql_get_last_message = mssql_get_last_message();
        $sQuery_added  = "BEGIN TRY\n";
        $sQuery_added .= "\t".$sQuery."\n";
        $sQuery_added .= "END TRY\n";
        $sQuery_added .= "BEGIN CATCH\n";
        $sQuery_added .= "\tSELECT 'Error: '  + ERROR_MESSAGE()\n";
        $sQuery_added .= "END CATCH";
        $rRun2= @mssql_query($sQuery_added, $hDb_conn);
        $aReturn = @mssql_fetch_assoc($rRun2);
        if(empty($aReturn))
        {
            echo $sError.'. MSSQL returned: '.$sMssql_get_last_message.'.<br>Executed query: '.nl2br($sQuery);
        }
        elseif(isset($aReturn['computed']))
        {
            echo $sError.'. MSSQL returned: '.$aReturn['computed'].'.<br>Executed query: '.nl2br($sQuery);
        }
        return FALSE;
    }
    else
    {
        return $rQuery;
    }
}
?>

Disclaimer: This is not the original function that I use in my project. The original function has Dutch text and uses some other functions that I have. I have removed my own functions and translated everything to English. That is everything I did. I only used a second function for the error messages. So I think everything should work, but I don't know for sure.
rodflash at rodflash dot com
23-Feb-2006 04: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 07: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 08: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;
    }
drunkennewfiemidget
22-Mar-2005 12: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
24-May-2002 10: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
27-Jan-2002 10: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 02: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.
06-Apr-2001 09: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.


只看该作者    顶部
离线 liyihongcug
高级会员



精华贴数 1
个人空间 0
技术积分 9573 (122)
社区积分 1043 (893)
注册日期 2004-7-15
论坛徽章:8
会员2007贡献徽章铁扇公主授权会员2008年新春纪念徽章开发板块每日发贴之星数据库板块每日发贴之星
开发板块每日发贴之星ITPUB新首页上线纪念徽章    

发表于 2008-1-23 14:00 

只看该作者    顶部
离线 liyihongcug
高级会员



精华贴数 1
个人空间 0
技术积分 9573 (122)
社区积分 1043 (893)
注册日期 2004-7-15
论坛徽章:8
会员2007贡献徽章铁扇公主授权会员2008年新春纪念徽章开发板块每日发贴之星数据库板块每日发贴之星
开发板块每日发贴之星ITPUB新首页上线纪念徽章    

发表于 2008-1-23 14:00 
mssql_ bind
mssql_ close
mssql_ connect
mssql_ data_ seek
mssql_ execute
mssql_ fetch_ array
mssql_ fetch_ assoc
mssql_ fetch_ batch
mssql_ fetch_ field
mssql_ fetch_ object
mssql_ fetch_ row
mssql_ field_ length
mssql_ field_ name
mssql_ field_ seek
mssql_ field_ type
mssql_ free_ result
mssql_ free_ statement
mssql_ get_ last_ message
mssql_ guid_ string
mssql_ init
mssql_ min_ error_ severity
mssql_ min_ message_ severity
mssql_ next_ result
mssql_ num_ fields
mssql_ num_ rows
mssql_ pconnect
mssql_ query
mssql_ result
mssql_ rows_ affected
mssql_ select_ db


只看该作者    顶部
离线 liyihongcug
高级会员



精华贴数 1
个人空间 0
技术积分 9573 (122)
社区积分 1043 (893)
注册日期 2004-7-15
论坛徽章:8
会员2007贡献徽章铁扇公主授权会员2008年新春纪念徽章开发板块每日发贴之星数据库板块每日发贴之星
开发板块每日发贴之星ITPUB新首页上线纪念徽章    

发表于 2008-1-23 14:01 
请教在一个页面里怎么样分别执行php的代码?
标签:  页面 字段 语句 请教 代码      


问:
例如某张表有字段A和B,
一个页面有2个下拉菜单,第一个是A的查询结果,第二个要求根据第一个的查询结果作为条件再查询B。请问用php什么样才能在一个页面里分别执行代码段?
______________________________________________________________________________________________
答1:
加个if判断,条件为真则执行第二个
______________________________________________________________________________________________
答2:
没错,先执行第一句的查迅,if(isset($A)){B语句};
______________________________________________________________________________________________
答3:
用javascript来做双关联下拉菜单!
______________________________________________________________________________________________
答4:
这个丢在客户端就OK了
______________________________________________________________________________________________
答5:
A查询后得到的结果是什么呢?是多条记录还是一条记录,还是只是为了返回一个逻辑值?
如果是一条记录或一个逻辑值,用if语句应该可以解决问题,如果是多条记录就用while语句。
______________________________________________________________________________________________
答6:
我已搞定代码如下:
表root.dept有两个字段dw和office。
<? require('conn.php'); ?>
<? $query1="SELECT distinct dw FROM root.dept";
   $result1=mssql_query($query1,$conn);
?>
<script language="JavaScript">
function chang()
{ for (i=1;i<=form1.selectkind.length;i++)
   {form1.selectkind.text="";
    form1.selectkind.value="";
        form1.selectkind.length--;
        form1.selectkind.length--;}
<? while ($row1=mssql_fetch_assoc($result1)):
$dw=$row1["dw"];       
?>       
if(form1.danwei.value=="<? echo $dw;?>")
   {<? $query2="select office from root.dept where dw='$dw'";
      $result2=mssql_query($query2,$conn);
while ($row2=mssql_fetch_assoc($result2)):
        $office=$row2["office"];
?>
   
         opt=new Option;
         opt.text="<? echo $office;?>";
         opt.value="<? echo $office;?>";
     form1.selectkind.add(opt);
          <? endwhile;?>
        }<? endwhile;?>
}
</script><form name="form1" method="post" action="">
  <select name="danwei" id="select2" onChange='chang()'>
    <option value="" selected>请选择</option>
        <?php
                  
$query_dept1 = "SELECT distinct dw FROM root.dept";
$dept1 = mssql_query($query_dept1, $conn) or die(mssql_error());
$row_dept1 = mssql_fetch_assoc($dept1);
$totalRows_dept1 = mssql_num_rows($dept1);
do {  
?>
    <option value="<?php echo $row_dept1['dw']?>"><?php echo $row_dept1['dw']?></option>
    <?php
} while ($row_dept1 = mssql_fetch_assoc($dept1));
  $rows = mssql_num_rows($dept1);
  if($rows > 0) {
      mssql_data_seek($dept1, 0);
          $row_dept1 = mssql_fetch_assoc($dept1);
  }
?>
  </select>
  <select name="selectkind" >
  </select>
</form>


只看该作者    顶部
 
    

相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问