2014-01-13 84 views
0

我運行這個PDO查詢在PHP中:PHP PDO SELECT查詢不顯示結果

$stmt = $pdo_conn->prepare("SELECT * from contacts where email = :email "); 
$stmt->execute(array(':email' => $from)); 
$contact = $stmt->fetchAll(PDO::FETCH_ASSOC); 
if(count($contact) > 0) { 
    echo $contact["email"]; 
} 

,但它不是附和從聯繫人表中的電子郵件列

我知道有挑釁的值有作爲如果我確實迴應「是」;在if語句裏面顯示

我在這裏做了什麼錯?

var_dump($ contact);顯示

array(1) { [0]=> array(22) { ["sequence"]=> string(3) "266" ["newsletter"]=> string(3) "yes" ["company_sequence"]=> string(3) "278" ["title"]=> string(2) "Mr" ["forename"]=> string(7) "Forename" ["surname"]=> string(4) "Surname" ["email"]=> string(22) "[email protected]" ["password"]=> string(32) "**********" ["phone"]=> string(0) "" ["mobile"]=> string(11) "00000000000" ["notes"]=> string(0) "" ["contactstatus"]=> string(0) "" ["logintries"]=> string(1) "0" ["dob"]=> string(10) "0000-00-00" ["receive_allticketupdates"]=> string(3) "yes" ["receive_accountsemails"]=> string(3) "yes" ["can_edit_contacts"]=> string(3) "yes" ["view_all_tickets"]=> string(3) "yes" ["receive_orderemails"]=> string(0) "" ["can_login_online"]=> string(3) "yes" ["last_satisfaction_survey_received"]=> string(10) "0000-00-00" ["receive_domainemails"]=> string(0) "" } } 
+0

嘗試在':: fetchAll()'之後運行'var_dump($ contact);'。這將幫助我們看到問題的可能性。 – Sam

+0

什麼是'var_dump($ contact)'? –

+0

檢查我的更新 – user2710234

回答

1

因爲您使用的是fetchAll(),即使只有一個結果,也會收到二維數組結果。另外

echo $contact[0]['email']; 

,如果你想/希望單行,你可以使用fetch()代替fetchAll()

要從此讓您的一個結果,你可以通過$contact[0]訪問它,而不是

$contact = $stmt->fetch(PDO::FETCH_ASSOC); 
if(count($contact) > 0) { 
    echo $contact["email"]; 
} 
1

看起來$contact將包含一個行數組。所以你需要訪問特定行的email字段。事情是這樣的:

echo $contact[0]['email']; 

或者使用一個循環:

if (!empty($contact)) { 
    foreach ($contact as $thisContact) { 
    echo $thisContact['email']; 
    } 
} 

或者,使用的fetchAssoc代替fetchAll

while ($contact = $stmt->fetch(PDO::FETCH_ASSOC)) { 
    echo $contact['email']; 
} 
0

fetchAll獲取數組中的所有行,所以結果多維數組。

您可能正在尋找echo $contact[0]["email"];