Here is my db class for connecting with database using pdo ext.
class db
{
private $host;
private $dbname;
private $username;
private $password;
public function __construct($host,$db,$name,$pass)
{
$this->host=$host;
$this->dbname=$db;
$this->username=$name;
$this->password=$pass;
$dsn = 'mysql:'.$this->dbname.';'.$this->host;
try
{
$conn = new PDO($dsn, $this->username, $this->password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo 'ERROR: ' . $e->getMessage();
}
}
}
I call db class in login class like this...
$host='localhost';
$db='techy_issue_tracker';
$name='root';
$pass='';
$base= new db($host,$db,$name,$pass);
Here is the problem, to make a pdo query inside login class (extending db class) if I use a line this...
$stmnt = $conn->prepare('SELECT id FROM users WHERE name :name OR email = :email');
It generates two errors, saying.. Notice: Undefined variable: conn... and Fatal error: Call to a member function prepare() on a non-object...
I can fix this by simply putting all pdo stuff inside login class but still I am just curious...how do you guys call an object (which is an instance of pdo class?) from another class.
PHP Class Based User System With PDO - Call to a member function prepare() on a non-object This question is interesting but didn't understand much :/
Didn't practice OOP much so some good explanation would be appreciated! Thanks.