pdo - OOP PHP error for non-object when trying to get user info from database -


this question has answer here:

i'm trying make oop class user info error

fatal error: call member function prepare() on non-object in functions.php

user.php - >

include_once 'functions.php'; $user = new user();  echo $user->get_fullname(5); 

functions.php ->

include_once 'database.php'; class user  {     public function connect()     {         $dbh = new db_class();     }      public function get_fullname($uid)     {          $getname = $dbh->prepare("select emailaddress users userid =:username");         $getname->bindparam(':username', $uid);         $getname->execute();         $rowname = $getname->fetch();         $email = $rowname['emailaddress'];         return $email;      }     } 

database.php - >

class db_class {     public function connect() {         try {             $dbh= new pdo("mysql:host=localhost;dbname=root",'users','password', array(                 pdo::attr_errmode => pdo::errmode_exception,                 pdo::mysql_attr_init_command => "set names utf8"             ));         } catch (pdoexception $e) {             echo $e->getmessage();         }          $dbh->setattribute(pdo::attr_errmode,pdo::errmode_warning);         $dbh->setattribute(pdo::attr_case,pdo::case_lower);     }  } 

what i'm doing wrong :(

you never give access pdo instance $dbh things youre trying use in. seems youre using classes simple groupings of functions expecting magic happen :-) how existingcode:

class db_class {      protected $dsn;     protected $user;     protected $password;      protected $connection;      public function __construct($dsn = null, $user = null, $password = null)     {         $this->dsn = $dsn;         $this->user = $user;         $this->password = $password;     }      public function connect()     {        if($this->connection) {          return $this->connection;        }         try {          $this->connection = new pdo($this->dsn,$this->user, $this->password,              array(               pdo::attr_errmode => pdo::errmode_exception,               pdo::mysql_attr_init_command => "set names utf8"          ));           $this->connection->setattribute(pdo::attr_errmode,pdo::errmode_warning);          $this->connection->setattribute(pdo::attr_case,pdo::case_lower);           return $this->connection;        }        catch (pdoexception $e) {            return false;        }      } }  class user {     protected $db;      public function __construct(db_class $db = null)      {         $this->db = $db;     }      public function setdb(db_class $db)     {        $this->db = $db;     }        public function get_fullname($uid)     {         $stmt = $this->db->connect()->prepare("select emailaddress users userid =:username");         $stmt->execute(array(':username', $uid));         if($row = $getname->fetch()) {            return $row['emailaddress'];         } else {            return null;         }     }  } 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -