I am trying to setup a PDO connection to replace my standard mysql connection. Until now I have a class called DB_Class that has in it's constructor the typical mysql_connect, mysql_select_db... stuff.
I use it by creating a new instance of DB_Class in the constructor of each of my objects that will hit the DB. I got the example online somewhere and it seems to work.
Now I want to switch to PDO and I am trying the same logic here but it is not working as I'd hoped. Here is what I have.
class DB_Class {
function __construct() {
//$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or
// die('Oops connection error -> ' . mysql_error());
//mysql_select_db(DB_DATABASE, $connection) or die('Database error -> ' . mysql_error());
$dbConnString = "mysql:host=" . DB_SERVER . ";port=".DB_PORT." dbname=" . DB_DATABASE;
$dbConnection = new PDO($dbConnString, DB_USERNAME, DB_PASSWORD);
//$dbConnection = new PDO('mysql:dbname='.DB_DATABASE.';host='.DB_SERVER.';charset=utf8', DB_USERNAME, DB_PASSWORD);
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
Here is the Constructor of my client class
//Initialize the DB Class
public function __construct() {
$db = new DB_Class();
}
Here is where I am not quite sure how to use it?
public function FindByOwnerID($id) {
$clients = array();
$stmt = $db->prepare("SELECT * FROM Client WHERE OwnerID = :id");
$stmt->execute(array(':id' => $id));