Hi all,
I am looking into PDO, as I want to learn how to use it for future projects.
I am fairly new to OOP, but got the basics by now..
All though I cant get this database class to work :
class Database {
private $host = 'localhost';
private $user = 'root';
private $pass = '';
private $dbname = 'cms';
private $dbh;
private $error;
public function __construct(){
// DSN :
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Options :
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Opret nyt PDO instance :
try {
return $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Eventuelle fejl :
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
}
$db = new Database();
$db->query("SELECT link, url FROM pages WHERE publiceret = 'ja'");
What needs to be fixed in order to use the connection?
Also, I want to use a persistent connection, so I dont create a new connection each time, is this the correct approach to do that? AND is it a good approach to enhance performance? Or is it not really gaining a lot...?
The attempt on the query above results in the following error:
Fatal error:
Call to undefined method Database::query() in C:\wamp\www\classLibrary\PDO\pdo_connection.php
on line 37
Best regards, Klemme