Franklin Noel
Franklin Noel

Reputation: 109

PDO::FETCH_OBJ php

PDO::FETCH_OBJ -> Returns an anonymous object with property names that correspond to the column names returned in your result set. For example:

 Array ( [0] => stdClass Object ( [id] => 3 [email] => [email protected] [password] => password [salt] => salt [devices] => 1 [accounttype] => 1 [accountcreated] => 2014-03-26 [confirmed] => 1 [confirmcode] => 0 [logindate] => 2014-03-26 ) )

How do I get the email value??????? Below is what i have so far

  $_db = DB::getInstance();
  $_db = $_db->query('SELECT * FROM users WHERE email = ? AND password = ? ', array($email, $pass));
  $results = $_db->results();
  print_r($results);
  print_r($results->email);  <<<<<<<<<<<--- does not work!

PLEASE HELP been at this for 24hours.... I know its simple because I have done it before... just cant seem to remember of find any referencing code!

Upvotes: 0

Views: 7278

Answers (2)

Gustavo
Gustavo

Reputation: 11

You should use something like the following:

foreach ($results as $row) {
        print_r($row->email);
}

If you expect to have just one row returned, it is always a good idea to check that. You can use count() with your $results variable like this:

if (count($results)==1) {
      print_r($results[0]->email);
}

Upvotes: 1

skrilled
skrilled

Reputation: 5371

You can see in what you pasted that $results is an array of objects, so you need to specify which item in the array to get a proper result for email.

Example:

print_r($results[0]->email);

Upvotes: 3

Related Questions