I use of this mvc. (here is the documentation). Now I want to create a module for connect to database and run the query. but there is a error:
Fatal error: Cannot access self:: when no class scope is active in {address(in db.class.php)}
What is the problem ??
db.class.php (in the model folder)
<?php
class db{
/*** Declare instance ***/
private static $instance = NULL;
/*
* @return object (PDO)
*
* @access public
*/
public static function getInstance() {
if (!self::$instance) {
function dataQuery($query, $params) {
// what kind of query is this?
$queryType = explode(' ', $query);
// establish database connection
try {
self::$instance = new PDO('mysql:host=localhost;dbname=spy', USER, PASS);
self::$instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo $e->getMessage();
$errorCode = $e->getCode();
}
// run query
try {
$queryResults = self::$instance->prepare($query);
$queryResults->execute($params);
if($queryResults != null && 'SELECT' == $queryType[0]) {
$results = $queryResults->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
$queryResults = null; // first of the two steps to properly close
$dbh = null; // second step tp close the connection
}
catch(PDOException $e) {
$errorMsg = $e->getMessage();
echo $errorMsg.'<br>';
}
}
}
return self::$instance;
}
} /*** end of class ***/
?>
init.php (in the includes )
<?php
function __autoload($class_name) {
$filename = strtolower($class_name) . '.class.php';
$file = __SITE_PATH . '/model/' . $filename;
if (file_exists($file) == false)
{
return false;
}
include ($file);
}
/*** a new registry object ***/
$registry = new registry;
/*** create the database registry object ***/
$registry->db = db::getInstance();
?>
index.php: (in the controller folder)
<?php
Class indexController Extends baseController {
public function index() {
error_reporting(E_ALL);
ini_set('display_errors', 1);
define('USER', 'root');
define('PASS', '');
function findKitByMfgPrice($mfg, $price) {
$query = "SELECT * FROM `test` WHERE `prod_name` LIKE ? AND `prod_price` > ?";
$params = array($mfg, $price);
$results = dataQuery($query, $params);
return $results;
}
$mfg = '%Mobeius%';
$price = 34.95;
$kitsByMfgPrice = findKitByMfgPrice($mfg, $price);
echo '<pre>';
$welcome = $kitsByMfgPrice;
/*** set a template variable ***/
$this->registry->template->welcome = $welcome;
/*** load the index template ***/
$this->registry->template->show('index1');
}
?>