2017-07-26 68 views
2

我目前正在爲我的系統建立數據庫,並希望通過使用存儲的函數和存儲過程爲每個來自php的請求創建數據庫的抽象。 我的問題是我有一個函數,檢查電子郵件是否存在,如果電子郵件存在則返回true,否則返回false。現在我怎樣才能檢查在php中的pdo調用後的布爾返回值。我的PHP代碼是檢查布爾返回值pdo的mysql存儲函數

$connection = new DB_CONNECTION(); 
$sql = "SELECT email_exists(:email) "; 
$placeholder = array(":email" => $userMail); 
$statement = $connection->prepare_query($sql); 
$result = $statement->execute($placeholder); 
$result = $statement->fetch(); 
echo $result; 
+0

陣列(大小= 1) 「email_exists(」 [email protected] 「)」 根據返回0或1 –

+0

'$值=($條件)=> '1')? TRUE:FALSE;'---'$ value =($ condition)? 1:0;'--- http://php.net/manual/en/language.operators.comparison.php --- http://php.net/manual/en/language.types.boolean.php - 根據*「我如何檢查布爾返回值」*。 –

+0

Chris85謝謝你,我正在尋找,如果thre是更清潔的方式來獲得結果,但這工作正常 –

回答

0

只要給返回值的alias,它是由數組中的索引訪問。

$connection = new DB_CONNECTION(); 
$sql = "SELECT email_exists(:email) as da_count "; 
$placeholder = array(":email" => $userMail); 
$statement = $connection->prepare_query($sql); 
$result = $statement->execute($placeholder); 
$result = $statement->fetch(); 
echo $result['da_count']; 

您可以根據需要進行條件化;

if(empty($result['da_count'])) { 
    echo 'Email doesnt exist'; 
} else { 
    echo 'Email does exist'; 
}