PHP !isset 在未设置时返回 true


PHP !isset returns true when not set

首先,我的代码:

    $stmt = $pdo->prepare("SELECT id FROM users WHERE
                                        username = :user_email OR
                                        email = :user_email");
        $user_email = $_POST["usernameemail"];
    $stmt->execute(array(
                                    'user_email'=>$user_email
                                    ));
    $account_exist = $stmt->fetch();
    if(!isset($account_exist)) {
        $account_exist_error = '<p style="color:#F00;">Username or Email not found</p>';
    }

当查询未找到任何内容时,$account_exist_error 不会返回

我也试过

if($account_exist === '') {
    ect
}

这些都没有返回任何内容,其他相关问题对我没有帮助,$account_exists = '' 在我的代码中设置了

通常,empty($var_name)会更可靠,对于任何null、空白或未定义的内容,都会返回 true。这甚至包括空数组。

但是,在这种情况下,您将返回一个数据库对象。对于 isset,它将始终返回 true,对于空返回 false。因此,您将需要使用数据库对象 API。在这种情况下,您应该使用 rowCount。

您可以使用

if(!$account_exist)

而不是

if(!isset($account_exist))

因为,它的返回值是

TRUE    Success. Data has been fetched
FALSE   Error occurred
NULL    No more rows/data exists or data truncation occurred

在这种情况下,falsenull 都是伪值,并且适用于!运算符。如果您使用

f(!isset($account_exist))

然后,它始终被设置,因为$account_exist包含一个值。

尝试

if ($account_exist == null)