db2_fetch_object(): 当查询在 strsql 中工作时抛出获取失败

db2_fetch_object(): Fetch Failure thrown when query works in strsql

在 PHP 中 运行 查询时出现以下错误:

Warning: db2_fetch_object(): Fetch Failure in ... on line 15

最重要的是运行当我在我的 I5 终端中使用 strsql 运行 相同的查询时它有效,并且我得到了一个很好的记录集。我运行的查询是SELECT * FROM TABLE(U8.feedconst(0)) AS t1.

U8.feedconst(0) 是驻留在 U8 库中的 table 函数,但会从用户的库中提取数据。当我使用 IBM 终端时,我将用户的库设置为 uasetlibl。该库在 $this->user->library 中的 PHP DB2 连接中指定(该库已在其他查询中成功使用)。

这里是 PHP 代码:

$this->conn = db2_connect(
        \HOST, 
        $this->user->username, 
        $this->user->password,
        array('i5_libl' => $this->user->library . ' ' . \PROD_LIB,
            'i5_naming' => DB2_I5_NAMING_ON)
);
$sql = 'SELECT * FROM TABLE(U8.feedconst(0)) AS t1';
$stmt = db2_prepare($this->conn, $sql);
if ($stmt) {
    $this->user->log('stmt = ' . strval($stmt), true);
    $exec = db2_execute($stmt);
    if ($exec) {
        $this->user->log('exec = ' . strval($exec), true);
        $this->user->log('db2_stmt_errormsg = ' . db2_stmt_errormsg($stmt), true);
        while ($row = db2_fetch_object($stmt)) {
            $this->user->log($sql, true);
        }
    }
}

日志显示在 db2_fetch_object() 行之前一切正常:

2015-11-18 10:43:53pm : stmt = Resource id #21
2015-11-18 10:43:53pm : exec = 1
2015-11-18 10:54:27pm : db2_stmt_errormsg = 

如您所见,我尝试使用 db2_stmt_errormsg() 从 DB2 获取真正的错误,但它没有返回任何内容。

我查看了其他类似的问题,例如 Warning: db2_fetch_assoc(): Fetch Failure and DB2 fetch failure error,但它们似乎没有解决我的情况。

我找到了悲伤的原因。 table 函数似乎在内部使用了一个额外的库。由于那个 table 函数在不同的库中有依赖关系,而另一个库没有包含在我的库列表中,返回到 PHP 的记录集是空的。

它在 i5 终端中工作,因为我的终端会话已经设置为在其库列表中包含其他库,我不知道。

当我在连接中将另一个内部库添加到我的库列表时,查询在 PHP 中工作得很好。

如果其他人遇到此问题,请确保您包含查询使用的所有库,包括任何可能是内部依赖项的库。