php - Undefined variable in object -
i querying database define variables:
$sth = $this->db->prepare( "select user_id users user_email = :user_email ;" ); $sth->execute( array( ':user_email' => $email_query ) ); $result = $sth->fetch();
however, when try use variable in function, returns $result->user_email undefined
.
$this->mp->createalias( $result->$user_email ); // $this->mp defined elsewhere
select user_id, users user_email = :user_email
your sql invalid, remove comma after user_id.
select user_id users user_email = :user_email
it's best use pdoexception. (e.g):
try { $sth = $this->db->prepare(" select user_id users user_email = :user_email "); $sth->execute(array( ':user_email' => $email_query )); $result = $sth->fetch(pdo::fetch_assoc); } catch(pdoexception $e) { echo $e->getmessage(); var_dump($e); }
the reason $user_email undefined because can't access returned array way. need access @ it's associative index. fetch()
returns array, need access array, not object.
if your're not getting property object, either undefined , not aware of it, or syntax wrong, or need set fetch type in pdo. try this:
$this->mp->createalias( $result['user_email'] );
Comments
Post a Comment