如何在PHP中处理来自db2_fetch_assoc的错误?

时间:2014-02-18 15:10:57

标签: php ibm-midrange db2-400

我查询了我在iSeries上运行的程序。我知道有时这个查询会偶尔填充,因为没有可供选择的记录。每次查询失败时,它都会以:

的形式登录到php.log
PHP Warning:  db2_fetch_assoc(): Fetch Failure in /www/zendsvr6/htdocs/views/hp1110.php on line 82

我正在寻找一种防止此错误记录的解决方案。我知道我已经可以禁用PHP错误日志记录了。我不想禁用日志记录。

以下是代码示例:

$stmt = db2_prepare($conn, $sql);

if (!$stmt) {
    $code = db2_stmt_error();
    $msg = db2_stmt_errormsg();
    //  TODO in production do not display SQL. Log it instead.
    die("Failed to prepare $sql. Reason: $code $msg");
} //(if (!$result))

$bound1 = db2_bind_param($stmt, 1, "item", DB2_PARAM_IN);
$bound2 = db2_bind_param($stmt, 2, "house", DB2_PARAM_IN);
$bound3 = db2_bind_param($stmt, 3, "counter", DB2_PARAM_INOUT);

if (!$bound1 || !$bound2 || !$bound3) {
    die("Failed to bind one or more parameters.");
} //(if (!$bound1 || !$bound2 || !$bound3) )

// execute with bound parameters.
$result = db2_execute($stmt);

if (!$result) {
    $code = db2_stmt_error();
    $msg = db2_stmt_errormsg();
    //  TODO in production do not display SQL. Log it instead.
    die("Failed to execute $sql. Reason: $code $msg");
} //(if (!$result))

if (!$counter) {
    return array();//
} //(if (!$counter))

// success. create array to return to caller.
$rows = array();
while (($row = db2_fetch_assoc($stmt)) != false) {
    $rows[] = $row;
} //(while...)

// this returns it to the caller.
return $rows;

} //(getParts)

谢谢!

1 个答案:

答案 0 :(得分:0)

如果db2驱动程序每次尝试从没有(更多)行可用的结果中获取时都会记录,那么您可以通过仅提取确切返回的行数来避免它:

$total_rows = db2_num_rows($stmt);
for ($i = 0; $i < $total_rows; $i++) {
   $row = db2_fetch_assoc($stmt);
   ... do stuff ...
}