簡體   English   中英

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_exist =''在我的代碼中已設置

通常, empty($var_name)會更可靠,如果為null ,blank或undefined,則返回true。 這甚至包括空數組。

但是,在這種情況下,您將返回一個數據庫對象。 isset總是返回true,而empty則返回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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM