I wrote this small class for my database connections in my project:
<?php
class DatabaseUtility{
private $dsn, $username, $password, $database, $pdo;
public function __construct($host = 'localhost', $username = 'root', $password = '', $database){
$this->dsn = "mysqli:dbname=$database;host:$host";
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
public function connect(){
try{
$this->pdo = new PDO($this->dsn,$this->username,$this->password,null);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $err){
die($err->getMessage());
}
}
public function prepareStatment($query){
$this->pdo->prepare($query);
}
}
?>
And here is how I use it:
<?php
require 'DatabaseUtility.php';
$db = new DatabaseUtility('localhost','root','','apex');
$db->connect();
$statment = $db->prepareStatment("Select offer_id from offer_images where img_id = :img_id");
?>
But I get the following error:
Could not find driver
I am new to PDO, so please help me, what am I doing wrong? Is this method suitable for safe and fast database operation?
Update:
I am using these lines of code to use my DatabaseUtility class, but I got an error:
<?php
require 'DatabaseUtility.php';
$id= 25;
$db = new DatabaseUtility('localhost','root','','apex');
$db->connect();
$statment = $db->prepareStatment("Select offer_id from offer_images where img_id = :img_id");
$statment->bindParam("img_id", $id ,PDO::PARAM_INT);
$statment->execute();
print_r($statment);
?>
Error:
call to a member function bindParam() on a non-object in this line:
$statment->bindParam("img_id", $id ,PDO::PARAM_INT);