Creating a PHP PDO database class, trouble with the OOP
this is my current Database class:
class Database {
private $db;
function Connect() {
$db_host = "localhost";
$db_name = "database1";
$db_user = "root";
$db_pass = "root";
try {
$this->db = new PDO("mysql:host=" . $db_host . ";dbname=" .
$db_name, $db_user, $db_pass);
} catch(PDOException $e) {
die($e);
}
}
public function getColumn($tableName, $unknownColumnName,
$columnOneName, $columnOneValue, $columnTwoName = "1", $columnTwoValue
= "1") {
$stmt = $this->db->query("SELECT $tableName FROM
$unknownColumnName WHERE $columnOneName='$columnOneValue' AND
$columnTwoName='$columnTwoValue'");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $results[0][$unknownColumnName];
}
}
I'm trying to run it using the following code:
$db = new Database();
$db->Connect();
echo $db->getColumn("Sessions", "token", "uid", 1);
And i get the following error:
PHP Fatal error: Call to a member function fetchAll() on a non-object in
/Users/RETRACTED/RETRACTED/root/includes/Database.php on line 19
Any idea what's up? Thanks
No comments:
Post a Comment