Below is the db connection class, but I am going to improve it by extending the PDO class itself:
<?php
class db{
private static $instance = NULL;
public function __construct() {
}
public static function getInstance() {
if (!self::$instance)
{
self::$instance = new PDO("mysql:host=localhost;dbname=dbmvc", 'root', '');;
self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
private function __clone(){
}
}
?>
extended class:
<?php
class Model extends db{
final public function fetchInfo()
{
global $pdo, $db;
$query = $db->prepare("SELECT name FROM table");
$query->execute();
$result = $query -> fetchAll();
foreach( $result as $row )
{
echo $row['name'];
}
}
}
?>
But I have errors when I call this method:
$model=new Model();
$model->fetchInfo();
error is:
Call to a member function prepare() on a non-object in C:\xampp\htdocs\mvc\model\model.class.php on line 11
I am new to PDO, and I trying to figure it out but I can't find solution anywhere, can anyone help. Tnx!