0

i am running this PDO Query in PHP:

$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"];
}

but its not echoing the email column from the contacts table

i know there is defiantly a value there as if i do echo 'yes'; inside the if statement it shows

what have i done wrong here?

var_dump($contact); shows

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) "" } }
2
  • Try running a var_dump($contact); after your ::fetchAll(). This will help us see where the problem may be. Commented Jan 13, 2014 at 21:09
  • what is var_dump($contact)? Commented Jan 13, 2014 at 21:11

3 Answers 3

1

Because you're using fetchAll(), you're receiving a 2-dimensional array of results even when you expect just one.

To get your single result from this, you can access it via $contact[0] instead:

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

Alternatively, if you want/expect a single row, you can use fetch() instead of fetchAll():

$contact = $stmt->fetch(PDO::FETCH_ASSOC);
if(count($contact) > 0) {
    echo $contact["email"];
}
Sign up to request clarification or add additional context in comments.

Comments

1

It seems that $contact will contain an array of rows. So you'll need to access the email field of a particular row. Something like this:

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

Or use a loop:

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

Or, using fetchAssoc instead of fetchAll:

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

Comments

0

fetchAll fetches all rows in an array, so the result a multi-dimensional array.

You are probably looking for echo $contact[0]["email"];.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.