【问题标题】:I'm getting a connection error with mysqli_stmt_init but no errors are displaying我收到 mysqli_stmt_init 的连接错误,但没有显示错误
【发布时间】:2022-01-23 23:31:40
【问题描述】:

我被难住了。我只需要有人查看我的代码并就连接不工作的原因给我一些想法。它适用于我拥有的所有其他更新功能(类似的代码/复制粘贴),但不是这个。

我还必须补充一点,它运行并更新数据库。我的问题是最后得到结果。我将错误追溯到连接,它已经没有任何意义了。

/**
 * Update Avatar Function
 */
function updateAvatar($conn, $username, $avatar) {
    // setup a query
    $sql = "UPDATE users SET avatar = ? WHERE username = ?;";

    // prepare a statement to send a sanitized query
    $stmt = mysqli_stmt_init($conn);
    
    // check for a fail in case the query is faulty
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("location: ../admin/account.php?error=stmtfailed");
        exit();
    }

    // bind our data to the statement and execute
    mysqli_stmt_bind_param($stmt, "ss", $avatar, $username);
    mysqli_stmt_execute($stmt);

    // save the avatar
    $avatar = json_decode($avatar, true);
    move_uploaded_file($avatar["tmp_name"][0], '../assets/images/uploads/' . $avatar["name"][0]);

    // grab the returned data
    $resultData = mysqli_stmt_get_result($stmt);

    // try to grab the row and return it
    if (!$row = mysqli_fetch_assoc($resultData)) {
        return $row;
    }

    // close the statement
    mysqli_stmt_close($stmt);

    // send the user to the account page with a success message
    header("location: ../admin/account.php?success=update");
}

【问题讨论】:

  • 您遇到的错误是什么?
  • 我认为你应该从这个声明中删除!,除非我误读了它:if (!$row = mysqli_fetch_assoc($resultData))。如果失败,或者没有返回结果,那么就没有什么可返回的了吗?
  • @ChrisHaas 应该删除整个if 语句,因为它没有任何意义。我认为这是 OP 正在谈论的错误
  • 是的,我现在正在阅读 SQL,我看到有一个 UPDATE。也许他们想要mysqli_stmt_affected_rows()
  • mysqli_stmt_get_result: For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN, mysqli_stmt_get_result() will return a mysqli_result object. For other successful queries, mysqli_stmt_get_result() will return false. 因此$resultData 将是错误的,mysqli_fetch_assoc($resultData) 很可能会抛出错误,而不是做任何你认为你想让它做的事情

标签: php mysqli


【解决方案1】:

您似乎一直在观看 Youtube 上那个臭名昭著的教程。你的代码太复杂了,你对自己在做什么感到困惑。

首先,确保您拥有enabled mysqli error reporting。只需在连接数据库前添加mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);即可,无需检查每个函数调用是否成功。

您的代码中的主要问题是您试图在 UPDATE 查询上调用 mysqli_stmt_get_result,这永远不会产生结果。只需删除所有这些代码。

function updateAvatar(mysqli $conn, $username, $avatar): never {
    // setup a query
    $sql = "UPDATE users SET avatar = ? WHERE username = ?;";
    $stmt = mysqli_prepare($conn, $sql);

    // bind our data to the statement and execute
    mysqli_stmt_bind_param($stmt, "ss", $avatar, $username);
    mysqli_stmt_execute($stmt);

    // save the avatar
    $avatar = json_decode($avatar, true);
    move_uploaded_file($avatar["tmp_name"][0], '../assets/images/uploads/' . $avatar["name"][0]);

    // send the user to the account page with a success message
    header("location: ../admin/account.php?success=update");
    // always exit after header location
    exit;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-17
    • 2020-12-27
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    相关资源
    最近更新 更多