ADODB / SQL Server mssql_execute 存储过程失败,但如果 debug 设置为 true 则可以工作

发布于 2024-10-12 03:43:31 字数 1640 浏览 0 评论 0原文

我正在使用 ADODB 从 PHP 连接到 SQL Server 数据库。我有几个执行得很好的存储过程。由于某种原因我创建的最后一个调用一直失败。我把 debug 设置为 true 并且它工作得很好。我不知道为什么会发生这种情况。

这是我得到的错误

Error: mssql_execute() [<a href='function.mssql-execute'>function.mssql-execute</a>]:      stored procedure execution failed  in /path/to/adodb/lib/adodb5/drivers/adodb-mssql.inc.php on line 768

这是我创建的用于将所有存储过程传递给的方法,就像我说的那样,它与其他存储过程一起工作得很好,但是这个方法和我已经仔细检查了参数和存储过程的所有拼写是否正确。

protected function _getSpArray($db, $sp, array $spParams) {
    try {
        $stmt = $db->PrepareSP($sp);
        foreach ($spParams as $param) {
            $db->InParameter($stmt, $param[0], $param[1]);
        }
        $db->SetFetchMode(ADODB_FETCH_ASSOC);
        $rs = $db->Execute($stmt);
        $rsArray = array();
        if (!$rs) {
            echo 'No records found \n';
            return;
        }
        else {
            foreach ($rs as $k => $row) {
                $rsArray[$k] = $row;
            }
        }
        return $rsArray;
    } catch (ErrorException $e) {
        echo $e->getMessage();
    }
}

它在 adodb5/drivers/adodb-mssql.inc.php 的 #768 行上失败

$rez = mssql_execute($sql[1]);

,并且 sql 数组具有这些值

[0] stored_procedure_name
[1] resource id='657' type='mssql statement'

我在 PHP.net 上看到了以下评论,我将我的 freetds 主机名更改为与IP 地址仍然没有。我不确定免费结果,因为我使用的是 adodb。

http://www.php.net/manual/en/function .mssql-execute.php#93938

我使用的是 adodb 5.11

I am connecting to a SQL Server database from PHP using ADODB. I have several stored procedures that I have been executing just fine. The last call that I have created for some reason keeps failing. I put debug as true and it works just fine. I have no idea why this would be happening.

here is the error I get

Error: mssql_execute() [<a href='function.mssql-execute'>function.mssql-execute</a>]:      stored procedure execution failed  in /path/to/adodb/lib/adodb5/drivers/adodb-mssql.inc.php on line 768

Here is the method I created to pass all my stored procedures to, like I said it works fine with other stored procedures but this one and I have double checked that all the spellings of the parameters and stored procedure are correct.

protected function _getSpArray($db, $sp, array $spParams) {
    try {
        $stmt = $db->PrepareSP($sp);
        foreach ($spParams as $param) {
            $db->InParameter($stmt, $param[0], $param[1]);
        }
        $db->SetFetchMode(ADODB_FETCH_ASSOC);
        $rs = $db->Execute($stmt);
        $rsArray = array();
        if (!$rs) {
            echo 'No records found \n';
            return;
        }
        else {
            foreach ($rs as $k => $row) {
                $rsArray[$k] = $row;
            }
        }
        return $rsArray;
    } catch (ErrorException $e) {
        echo $e->getMessage();
    }
}

It is failing on this line #768 of adodb5/drivers/adodb-mssql.inc.php

$rez = mssql_execute($sql[1]);

and the sql array has these values

[0] stored_procedure_name
[1] resource id='657' type='mssql statement'

I have seen the following comments on PHP.net, I changed my freetds host name to be something different then the IP address and still nothing. I am not sure about the free result since I am using adodb.

http://www.php.net/manual/en/function.mssql-execute.php#93938

I am using adodb 5.11

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

佼人 2024-10-19 03:43:31

当您将 debug 设置为 true 时,ADODB 使用其他函数来执行该语句。在本例中 function _adodb_debug_execute(&$zthis, $sql, $inputarr)

如果 debug 设置为 false,ADODB 使用 function &_Execute($sql,$inputarr=false)代码>.当您检查这两种方法的来源时,您可以清楚地看到差异。

<?php
function _adodb_debug_execute(&$zthis, $sql, $inputarr)
{
    //ADODB prepares debug information dump...

    $qID = $zthis->_query($sql,$inputarr);

    //Here ADODB makes the difference

    if ($zthis->databaseType == 'mssql') { 
    // ErrorNo is a slow function call in mssql, and not reliable in PHP 4.0.6
        if($emsg = $zthis->ErrorMsg()) {
            if ($err = $zthis->ErrorNo()) ADOConnection::outp($err.': '.$emsg);
        }
    } else if (!$qID) {
        ADOConnection::outp($zthis->ErrorNo() .': '. $zthis->ErrorMsg());
    }

    if ($zthis->debug === 99) _adodb_backtrace(true,9999,2);
    return $qID;
}
?>

这是 _Execute 函数。

<?php
function &_Execute($sql,$inputarr=false){
        //Here ADODB chooses which fn to use
        if ($this->debug) {
            global $ADODB_INCLUDED_LIB;
            if (empty($ADODB_INCLUDED_LIB)) include(ADODB_DIR.'/adodb-lib.inc.php');
            $this->_queryID = _adodb_debug_execute($this, $sql,$inputarr);
        } else {
            $this->_queryID = @$this->_query($sql,$inputarr);
        }
        //...

        if ($this->_queryID === false) { // error handling if query fails
            //If debug ADODB prints backtrace regardless the result
            if ($this->debug == 99) adodb_backtrace(true,5);    
            $fn = $this->raiseErrorFn;
            if ($fn) {
                $fn($this->databaseType,'EXECUTE',$this->ErrorNo(),$this->ErrorMsg(),$sql,$inputarr,$this);
            } 
            $false = false;
            //Returns false no matter what...
            return $false;
        } 
        //...
    }
?>

尝试将其添加到脚本中以测试脚本的行为,并记住,如果执行失败,它将返回 false 值。所以要注意返回值。

protected function _getSpArray($db, $sp, array $spParams) {
    try {
        $stmt = $db->PrepareSP($sp);
        foreach ($spParams as $param) {
            $db->InParameter($stmt, $param[0], $param[1]);
        }
        $db->SetFetchMode(ADODB_FETCH_ASSOC);
        $rs = $db->Execute($stmt);
        $rsArray = array();
        if (!$rs) {
            echo 'No records found \n';
            return;
        }
        else {
            foreach ($rs as $k => $row) {
                $rsArray[$k] = $row;
            }
        }
        //add this line to free the resources after use. 
        mssql_free_result($rs);
        return $rsArray;
    } catch (ErrorException $e) {
        echo $e->getMessage();
    }
}

希望有帮助!!

When you set debug as true ADODB uses other function to execute the statement. In this case function _adodb_debug_execute(&$zthis, $sql, $inputarr)

If debug is set to false ADODB uses function &_Execute($sql,$inputarr=false). When you check the source for both methods you can clearly see the difference.

<?php
function _adodb_debug_execute(&$zthis, $sql, $inputarr)
{
    //ADODB prepares debug information dump...

    $qID = $zthis->_query($sql,$inputarr);

    //Here ADODB makes the difference

    if ($zthis->databaseType == 'mssql') { 
    // ErrorNo is a slow function call in mssql, and not reliable in PHP 4.0.6
        if($emsg = $zthis->ErrorMsg()) {
            if ($err = $zthis->ErrorNo()) ADOConnection::outp($err.': '.$emsg);
        }
    } else if (!$qID) {
        ADOConnection::outp($zthis->ErrorNo() .': '. $zthis->ErrorMsg());
    }

    if ($zthis->debug === 99) _adodb_backtrace(true,9999,2);
    return $qID;
}
?>

Here is the _Execute function

<?php
function &_Execute($sql,$inputarr=false){
        //Here ADODB chooses which fn to use
        if ($this->debug) {
            global $ADODB_INCLUDED_LIB;
            if (empty($ADODB_INCLUDED_LIB)) include(ADODB_DIR.'/adodb-lib.inc.php');
            $this->_queryID = _adodb_debug_execute($this, $sql,$inputarr);
        } else {
            $this->_queryID = @$this->_query($sql,$inputarr);
        }
        //...

        if ($this->_queryID === false) { // error handling if query fails
            //If debug ADODB prints backtrace regardless the result
            if ($this->debug == 99) adodb_backtrace(true,5);    
            $fn = $this->raiseErrorFn;
            if ($fn) {
                $fn($this->databaseType,'EXECUTE',$this->ErrorNo(),$this->ErrorMsg(),$sql,$inputarr,$this);
            } 
            $false = false;
            //Returns false no matter what...
            return $false;
        } 
        //...
    }
?>

Try adding this to your script to test the behaviour of the script and keep in mind that if the execute fails it will return a false value. So take care with the returned value.

protected function _getSpArray($db, $sp, array $spParams) {
    try {
        $stmt = $db->PrepareSP($sp);
        foreach ($spParams as $param) {
            $db->InParameter($stmt, $param[0], $param[1]);
        }
        $db->SetFetchMode(ADODB_FETCH_ASSOC);
        $rs = $db->Execute($stmt);
        $rsArray = array();
        if (!$rs) {
            echo 'No records found \n';
            return;
        }
        else {
            foreach ($rs as $k => $row) {
                $rsArray[$k] = $row;
            }
        }
        //add this line to free the resources after use. 
        mssql_free_result($rs);
        return $rsArray;
    } catch (ErrorException $e) {
        echo $e->getMessage();
    }
}

Hope it helps!!

画▽骨i 2024-10-19 03:43:31

好的,在找到 php.net 注释后,尽管我的 freetds 没有设置正确的名称,但我使用的是 IP 地址。我也没有使用 V 8.0,而是使用 7.0,直到此时,商店程序一切都工作正常。我尝试了评论和 DarkThrones 答案中提供的 mssql_free_result 解决方案,但这不起作用,但是当我使用 adodb 的 close() 方法时,它正在工作。它使用免费结果并传递查询 ID。

如果有人可以评论为什么这是必要的,那就太好了。是因为我用了太多内存还是什么?

Ok, after finding the php.net comments, even though my freetds wasn't setup with the proper name, I was using the ip address. I also wasn't using the V 8.0 I was using 7.0 and everything was working fine up until this point with the store procedures. I tried the mssql_free_result solution provide in the comments and DarkThrones answer and that didn't work but when I used adodb's close() method it is working. It uses free result and passes a query id.

If someone can comment as to why this is necessary that would be great. Is it because I was using up to much memory or something?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文