How can I use global DB variable inside class? Let's say I have this in my config.php
$dbh = new PDO("mysql:host=localhost;dbname=mydb", "root", "");
and I want to use this $dbh inside class as follows (MyClass.php)
class MyClass
{
public function DoSomething($plogin_id)
{
$sql = "SELECT * FROM mytable WHERE login_id = :login_id";
$stmt = $dbh->prepare($sql); //line 14
$stmt->bindParam(':login_id', $plogin_id, PDO::PARAM_STR);
}
}
And inside my index.php file I am using this MyClass as follows:
include "config.php";
$MyObject = new MyClass();
$login_result = $MyObject->DoSomething("admin");
It is giving me error:
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\MyProject\admin\includes\classes\MyClass.php on line 14