2013-12-10 23 views
0

如果我從MySQL數據庫獲取結果NULL值那麼PHP與它一起工作字符串值。測試空值返回我的不實和呼應返回NULL如何從dbs獲取NULL

$stmt = $mysqli->prepare("SELECT `activation` 
    FROM `authentication` 
    WHERE `mail`=?"); 

$stmt->bind_param('s', $mail); 
$stmt->execute(); 
$stmt->bind_result($result); 
$stmt->fetch(); 
$stmt->close(); 

if (is_null($result)) { 
    echo 'is NULL'; 
} else echo 'not NULL'; 

從這個例子回報將是不是NULL,但我希望得到爲NULL

+0

爲什麼你需要知道它是特別爲空?爲什麼不使用'empty($ result)'而不是? –

+0

,因爲empty()也返回FALSE。 – DimmuBoy

+0

呃...那它包含了什麼? –

回答

0

問題是與保存在數據庫值。我做更新記錄,現在它正在工作。 感謝您的幫助!

0
$sql = "SELECT `activation` 
    FROM `authentication` 
    WHERE `mail` "; 

if (!is_null($mail) { 
    $stmt = $mysqli->prepare($sql); 
    $sql .= " = ?"; 
    $stmt->bind_param('s', $mail); 
} 
else { 
    $stmt = $mysqli->prepare($sql); 
    $sql .= " ?"; 
    $stmt->bind_param('s', 'IS NULL'); 
} 

$stmt->execute(); 
$stmt->bind_result($result); 
$stmt->fetch(); 
$stmt->close(); 

if (is_null($result)) { 
    echo 'is NULL'; 
} else echo 'not NULL'; 

如果我沒有誤認爲連接到一個字符串的空值產生沒有輸出,所以你必須明確地說IS NULL

此外

在MySQL的陳述是不相同的

select * from config where description = null; 
Empty set (0.00 sec) 

select * from config where description is null; 
18 rows in set (0.00 sec) 
+0

我不確定是否連接到函數的返回是很好的做法,您能提供一個參考嗎? –

+0

我想測試$ result不是$ mail,因爲我的$ mail變量對於stmt不是null,但是我的結果來自'activation'可以是NULL – DimmuBoy

+0

嘗試定義$ result = null;在將它傳遞給bind_result之前 – robbmj