Php 是否有正确的方法使用$stmt->;close();

Php 是否有正确的方法使用$stmt->;close();,php,mysqli,Php,Mysqli,我在我的网站上有许多声明,我想知道您何时以及如何使用$stmt->close()正确。它会通过保持开放来打开漏洞吗 在本例中,关闭语句的正确位置是第23行吗 // First, check if the email and code exists if (isset($_GET['email'], $_GET['code'])) { if ($stmt = $con->prepare('SELECT * FROM accounts WHERE email = ? AND activ

我在我的网站上有许多声明,我想知道您何时以及如何使用
$stmt->close()正确。它会通过保持开放来打开漏洞吗

在本例中,关闭语句的正确位置是第23行吗

// First, check if the email and code exists
if (isset($_GET['email'], $_GET['code'])) {
    if ($stmt = $con->prepare('SELECT * FROM accounts WHERE email = ? AND activation_code = ?')) {
        $stmt->bind_param('ss', $_GET['email'], $_GET['code']);
        $stmt->execute();
        $stmt->store_result();

        if ($stmt->num_rows > 0) {
            // Account exists with the requested email and code
            if ($stmt = $con->prepare('UPDATE accounts SET activation_code = ? WHERE email = ? AND activation_code = ?')) {
                // Set the new activation code to 'activated', this is how we can check if the user has activated their account
                $newcode = 'activated';
                $stmt->bind_param('sss', $newcode, $_GET['email'], $_GET['code']);
                $stmt->execute();

                header('Location: messages.php?message=activated');
                exit;
            }
        } else {
            header('Location: messages.php?message=activated-error');
            exit;
        }
    }
}

这里有两个语句,我可以关闭它们吗?或者我只是在底部关闭它们?另外,当我使用
标题('Location:')
时,
$stmt->close()是否实际执行?

您不需要使用
$stmt->close()完全正确。在PHP中,几乎不需要手动关闭任何东西。一旦不再需要它,PHP将为您关闭它。如果您的代码结构正确,PHP将在最佳情况下为您关闭一切

使用
标题('Location:')
不会影响mysqli对象。当您退出代码时,整个脚本将停止,也就是说,如果尚未关闭,PHP将关闭所有内容


您真的应该使用一些封装。不要直接使用mysqli方法。创建一些函数或类,这些函数或类将从这个接口中抽象出来,这样您就更容易使用它了。如果操作正确,则无需担心关闭对象。

不应检查
prepare
的返回值。删除
if
语句。如果您还没有这样做,请启用正确的错误报告。你是说我的代码可能存在漏洞吗?不是。我是在建议如何编写更好更简单的代码。