2014-01-13 170 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"];