【问题标题】:How can a handle errors from db2_fetch_assoc in PHP?如何在 PHP 中处理来自 db2_fetch_assoc 的错误?
【发布时间】:2014-02-18 15:10:57
【问题描述】:

我有一个对我在 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)

谢谢!

【问题讨论】:

  • 请给出出现错误的代码
  • 好的,我把代码贴出来了。

标签: php ibm-midrange db2-400


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 2019-08-26
    • 1970-01-01
    相关资源
    最近更新 更多