理解php成员系统中的一个错误
我正在使用会话、函数和MySQL查询在php中构建一个成员系统, 我遇到了一个问题或错误,我不理解其含义以及如何纠正它。 在这件事上的任何帮助都将不胜感激 错误读取理解php成员系统中的一个错误,php,mysql,fatal-error,Php,Mysql,Fatal Error,我正在使用会话、函数和MySQL查询在php中构建一个成员系统, 我遇到了一个问题或错误,我不理解其含义以及如何纠正它。 在这件事上的任何帮助都将不胜感激 错误读取 Fatal error: Call to a member function error() on a non-object in /home/ob219/public_html/membership/index.php on line 6 $user = DB::getInstance()->get('users', ar
Fatal error: Call to a member function error() on a non-object in /home/ob219/public_html/membership/index.php on line 6
$user = DB::getInstance()->get('users', array('username', '=', 'ben'));
if($user->error()) {
echo 'No user';
} else {
echo 'OK!';
}
索引代码
$user = DB::getInstance()->get('users', array('username', '=', 'ben'));
if($user->error()) {
echo 'No user';
} else {
echo 'OK!';
}
db.php函数
$user = DB::getInstance()->get('users', array('username', '=', 'ben'));
if($user->error()) {
echo 'No user';
} else {
echo 'OK!';
}
public function action($action, $table, $where = array()) {
if(count($where) === 3) {
$operators = array('=', '>', '<', '>=', '<=');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if(in_array($operator, $operators)) {
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if(!$this->query($sql, array($value))->error()) {
return $this;
}
}
}
return false;
}
public function get($table, $where) {
return $this->action('SELECT *', $table, $where);
}
public function delete($table, $where) {
return $this->action('DELETE', $table, $where);
}
public function error() {
return $this->_error;
}
<?php
require_once 'core/init.php';
$user = DB::getInstance()->get('Users', array('username', '=', 'ben'));
if((!$user->count())) {
echo 'No user';
} else {
echo 'OK!';
}
公共函数操作($action,$table,$where=array()){
如果(计数($where)==3){
$operators=array(“=”、“>”、“=”、“是一个简单的错误,我把用户作为表名,在index.php中我有用户
解决方案
$user = DB::getInstance()->get('users', array('username', '=', 'ben'));
if($user->error()) {
echo 'No user';
} else {
echo 'OK!';
}
public function action($action, $table, $where = array()) {
if(count($where) === 3) {
$operators = array('=', '>', '<', '>=', '<=');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if(in_array($operator, $operators)) {
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if(!$this->query($sql, array($value))->error()) {
return $this;
}
}
}
return false;
}
public function get($table, $where) {
return $this->action('SELECT *', $table, $where);
}
public function delete($table, $where) {
return $this->action('DELETE', $table, $where);
}
public function error() {
return $this->_error;
}
<?php
require_once 'core/init.php';
$user = DB::getInstance()->get('Users', array('username', '=', 'ben'));
if((!$user->count())) {
echo 'No user';
} else {
echo 'OK!';
}
错误基本上是告诉你$user
不是一个对象。它可以是null
,false
等等。它意味着$user
是false
或null
。这不是一个对象,它没有error
函数。你的get()
函数(调用action()
)返回了false
或NULL
。很可能您的查询失败,并且您的返回$this
从未被调用。@Rocket Hazmat谢谢您,我将重新检查我的查询,然后再次检查