【问题标题】:PHP Prepared Statement Failing When Calling MySQL Function调用 MySQL 函数时 PHP 准备语句失败
【发布时间】:2016-07-26 22:03:40
【问题描述】:

我一直在使用准备好的语句,现在没有任何问题,但是今天当我尝试从准备好的语句中调用 MySQL 函数时,我得到以下信息:

Fatal error: Call to a member function fetch_array() on boolean in DB.php on line 336

这是我正在使用的代码,与我通常的 SELECTUPDATEDELETE 查询没有什么不同,我调用程序也没有问题,因为我的程序都没有返回任何值。

$sql = "SELECT FN_MAINTAIN_ASSET(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) AS assetId;";

try {
    $conn = $this->open();

    $stmt = $conn->prepare($sql);
    if($stmt === false) {
        trigger_error(sprintf($txt["error_sql"], $conn->error), E_USER_ERROR);
    }

    $stmt->bind_param('iiiisssbbiiiis', $modify, $type, $category, $year, $title, $description, $imageFilename, $imageMain, $imageThumbnail, $membersOnlyView, $privateView, $status, $memberId, $createdIp);
    $stmt->execute();

    $rs = $stmt->get_result();
    $result = $rs->fetch_array(MYSQL_ASSOC);  // Line 336
    $rs->free();

    $stmt->close();
    $this->close($conn);
} catch (Exception $e) {
    $logObj->error($e->getMessage());
}

$rs 在第 336 行之前为空,没有来自$stmteither 的错误消息。

如果有人有任何建议或想法,不胜感激。

【问题讨论】:

  • 在执行下一条指令之前检查$rs的值
  • 您是否明确启用了异常?如果不是,那么 try/catch 就没有用了,因为 mysqi/pdo 默认通过返回布尔值 false 来失败,而不是抛出异常。要么打开异常,要么开始显式检查返回值。

标签: php mysql prepared-statement stored-functions


【解决方案1】:

来自 get_result 的手册:

返回值

为成功的 SELECT 查询返回一个结果集,或者为其他查询返回 FALSE DML 查询或失败。 mysqli_errno() 函数可用于 区分这两种类型的故障。

如果您的语句返回布尔值,则它失败。检查来自$stmt->error 的错误消息,并更正任何错误。

【讨论】:

    猜你喜欢
    • 2018-02-21
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多