Fatal error: Call to a member function prepare()?
Fatal error: Call to a member function prepare()?
我正在尝试使用 PDO 查询数据库,但我遇到了一个致命错误
任何人帮助实际发生的事情...
Config.php
<?php
class Config{
public static function get($path = null){
if($path){
$config = $GLOBALS['config'];
$path = explode('/',$path);
foreach ($path as $bit) {
if (isset($config[$bit])) {
$config = $config[$bit];
}
}
return $config;
}
return false;
}
}
init.php
<?php
session_start();
$GLOBALS['config'] = array(
'mysql' => array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => 'rajaraman',
'db' => 'sms'
),
'remember' => array(
'cookie_name' => 'hash' ,
'cookie_expiry' => 604800
),
'session' => array(
'session_name' => 'user'
)
);
spl_autoload_register(function($class){
require_once 'classes/' .$class. '.php';
});
require_once 'functions/sanitize.php';
?>
index.php
<?php
require_once 'core/init.php';
$user = Db::getInstance()->query("SELECT username FROM users WHERE username = ?",array('raja'));
if ($user->error)
{
echo "No user";
}
else{
echo "OK!";
}
?>
?>
Db.php
<?php
class Db
{
private static $_instance = null;
private $_pdo,
$_query,
$_error=false,
$_results,
$_count=0;
private function __constructs()
{
try
{
$this->_pdo =new PDO("mysql:host=" .Config::get('mysql/host') . ";dbname=" .Config::get('mysql/db'),Config::get('mysql/username'),Config::get('mysql/password'));
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
public static function getInstance()
{
if (!isset(self::$_instance))
{
self::$_instance = new Db();
# code...
}
return self::$_instance;
}
public function query($sql,$params=array())
{
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql))
{
$x=1;
if (count($params))
{
foreach ($params as $param )
{
$this->_query->bindvalue($x,$param);
$x++;
}
}
if ($this->_query->execute())
{
$this->_results = $this->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
}
else
{
$this->error=true;
}
}
return $this;
}
public function error(){
return $this->error;
}
}
?>
当我 运行 使用 xampp 服务器时它显示错误,这是非对象上的致命错误...它在 prepare 方法中的实际错误。
致命错误:在 C:\xampp\htdocs\Student Management system\classes\Db.php 中的非对象上调用成员函数 prepare() 第 35 行
"Call to a member function prepare() on a non-object..."表示$this->_pdo->prepare($sql))
中的对象“_pdo”不存在。
反过来,这意味着构造函数(应该初始化 _pdo)从未被调用。
因为它应该被命名为 __construct()(而不是“__constructs”)。
此外,正如 Fred -ii- 已经指出的那样,bindValue() 拼写错误。
并且,为了完整起见:
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
我正在尝试使用 PDO 查询数据库,但我遇到了一个致命错误 任何人帮助实际发生的事情...
Config.php
<?php
class Config{
public static function get($path = null){
if($path){
$config = $GLOBALS['config'];
$path = explode('/',$path);
foreach ($path as $bit) {
if (isset($config[$bit])) {
$config = $config[$bit];
}
}
return $config;
}
return false;
}
}
init.php
<?php
session_start();
$GLOBALS['config'] = array(
'mysql' => array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => 'rajaraman',
'db' => 'sms'
),
'remember' => array(
'cookie_name' => 'hash' ,
'cookie_expiry' => 604800
),
'session' => array(
'session_name' => 'user'
)
);
spl_autoload_register(function($class){
require_once 'classes/' .$class. '.php';
});
require_once 'functions/sanitize.php';
?>
index.php
<?php
require_once 'core/init.php';
$user = Db::getInstance()->query("SELECT username FROM users WHERE username = ?",array('raja'));
if ($user->error)
{
echo "No user";
}
else{
echo "OK!";
}
?>
?>
Db.php
<?php
class Db
{
private static $_instance = null;
private $_pdo,
$_query,
$_error=false,
$_results,
$_count=0;
private function __constructs()
{
try
{
$this->_pdo =new PDO("mysql:host=" .Config::get('mysql/host') . ";dbname=" .Config::get('mysql/db'),Config::get('mysql/username'),Config::get('mysql/password'));
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
public static function getInstance()
{
if (!isset(self::$_instance))
{
self::$_instance = new Db();
# code...
}
return self::$_instance;
}
public function query($sql,$params=array())
{
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql))
{
$x=1;
if (count($params))
{
foreach ($params as $param )
{
$this->_query->bindvalue($x,$param);
$x++;
}
}
if ($this->_query->execute())
{
$this->_results = $this->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
}
else
{
$this->error=true;
}
}
return $this;
}
public function error(){
return $this->error;
}
}
?>
当我 运行 使用 xampp 服务器时它显示错误,这是非对象上的致命错误...它在 prepare 方法中的实际错误。
致命错误:在 C:\xampp\htdocs\Student Management system\classes\Db.php 中的非对象上调用成员函数 prepare() 第 35 行
"Call to a member function prepare() on a non-object..."表示$this->_pdo->prepare($sql))
中的对象“_pdo”不存在。
反过来,这意味着构造函数(应该初始化 _pdo)从未被调用。
因为它应该被命名为 __construct()(而不是“__constructs”)。
此外,正如 Fred -ii- 已经指出的那样,bindValue() 拼写错误。
并且,为了完整起见:
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);