诀窍重置odbc_error()

Álv*_*lez 11 php odbc

我已经知道,odbc_execute()当它返回时并不总是触发正确的ODBC错误FALSE(至少不是Oracle驱动程序),我无法完全信任odbc_error()odbc_errormsg().当没有先前的错误因为odbc_error()返回空字符串时,很容易检测到这种情况.但是,当它返回一些东西时,我不知道它是否属于最后一次失败的操作,或者它是前一个错误的遗留物.

最简单的解决办法是重置odbc_error()odbc_errormsg()函数时有一个错误,以便下一次调用将从头开始,但我无法找到一个支持的方式这样做.你能找到办法吗?

背景:我正在使用封装数据库调用的类来增强遗留应用程序.这就是为什么我需要尽可能地使一切都变得通用.

isa*_*isa 1

odbc_error 有时会变得令人困惑。执行的sql字符串和错误信息可能不同。为了防止这种情况,我们可以将所有执行的sql保存在一个数组中,当所有执行完成后,我们可以检查错误信息是什么。

首先让我们定义一个executeSQL 类,它将保存执行的sql 信息:

class executedSQL 
{
    public sql;
    public result;
    public error;
    public message;
}
Run Code Online (Sandbox Code Playgroud)

此类将保存所有 sql 信息及其结果和返回的消息。

如果我们使用一个类来连接odbc db:

class myODBC
{
    //holds the connection
    public $connection;

    //all executed sql string are added to this array as executedSQL object.
    public $executedSQLs = array();


    public function connect()
    {
        $this->connection = dbc_connect(" ", " ","") or die(odbc_errormsg());
    }

    public function execute($sql)
    {
        $execution = odbc_exec($this->connection, $sql); //execute it

        //put all the results to executedSQL object
        $executed = new executedSQL();
        $executed->sql = $sql;
        $executed->result = $execution;
        $executed->error = odbc_error();
        $executed->message = odbc_errormsg();

        //push to executedSQLs var.
        array_push($this->executedSQLs, $executed);

        return $execution;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我们执行我们的sql:

$db = new myODBC();

$db->connect();

$db->execute("select * from table1");
$db->execute("this is gonna be failed sql");
$db->execute("select * from table2");

print_r($db->executedSQLs);
Run Code Online (Sandbox Code Playgroud)

这将打印所有 sql 及其结果。此时我们可以看到执行的sql以及相关的错误信息。因此,从字面上看,我们并没有重置 odbc_error,但我们让它更清楚。如果错误信息重复两次,则更有可能是之前执行过的sql。这样调试就变得更容易了。