我使用this mvc。 (这是documentation)。现在我想创建一个连接数据库的模块并运行查询。但是有一个错误:
致命错误: {address(在db.class.php中)}
中没有类作用域活动时,无法访问self ::
问题是什么?
db.class.php (在模型文件夹中)
<?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 (在包含中)
<?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:(在控制器文件夹中)
<?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');
}
?>
答案 0 :(得分:0)
您的数据库应该以这种方式使用:
db.php:
class db{
/*** Declare instance ***/
private static $instance = NULL;
/*
* @return object (PDO)
*
* @access public
*/
public static function getInstance() {
if (!self::$instance) {
// 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();
}
}
return self::$instance;
}
public static function dataQuery($query, $params) {
// what kind of query is this?
$queryType = explode(' ', $query);
// run query
try {
$queryResults = self::getInstance()->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>';
}
} /*** end of class ***/
并在索引中使用它:
function findKitByMfgPrice($mfg, $price) {
$query = "SELECT * FROM `test` WHERE `prod_name` LIKE ? AND `prod_price` > ?";
$params = array($mfg, $price);
$results = db::dataQuery($query, $params);
return $results;
}
这应该是一个好的开始。但它仍然看起来很奇怪...... dataQuery
方法是错误的,它不应该是db类的一部分,它应该是你的索引的本地方法我认为......必须考虑一下...... < / p>