gpt4 book ai didi

Phpacademy OOP 登录和注册第 8/23 部分函数 error() 在非对象行 56 上

转载 作者:行者123 更新时间:2023-11-29 23:16:19 26 4
gpt4 key购买 nike

我目前正在学习教程 in this video series我收到此错误,但我不知道这里发生了什么。我已经按照教程中的代码查看了所有评论以及之前遇到问题的人的问答,但它也没有解决我的问题。

我得到的错误如下:

Fatal error: Call to a member function error() on a non-object in C:\wamp\www\ooplr\classes\DB.php on >line 56

这是我在 db.php 中第 45-65 行的代码

public function action($action,$table,$where =array()){
if(count($where)===3){
$operators=array('=','>','<','>=','<=');

$fields =$where[0];
$operator=$where[1];
$value =$where[2];

if(in_array($operator,$operators)){
$sql="{$action} FROM {$table} WHERE {$fields}{$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 error(){
return $this->_error;
}
public function query($sql,$params =array()){
$this->_error =false;
if($this->_query =$this->_pdo ->prepare($sql)){
if(count($params)){
$x = 1;
foreach($params as $param){
$this->_query->bindValue($x,$param);
$x++;
}
}
if($this ->_query->execute()){
$this->_result=$this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count=$this->_query->rowCount();
}else{
$this->_error =true;
}
}

这是我运行导致该错误的查询的index.php

<?php
require_once 'core/init.php';

$user = DB::getInstance()->get('users', array('username', '=', 'alex'));

if(!$user->count()) {
echo 'No user';
} else {
echo 'OK!';
}

?>

init.php 我已经检查了连接到数据库的所有详细信息是否正确

<?php
session_start();

$GLOBALS['config'] = array(
'mysql' => array(
'host'=> '127.0.0.1',
'username' => 'root',
'password'=>'',
'db' => 'ooplr'
),
'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';

?>

这是我现在遇到的错误 1st error that get

我检查了所有的拼写错误,数据库没问题,但仍然无法弄清楚 DB.php 第 56 行出了什么问题,对此有什么想法吗?

编辑:更改此行后

if(!$this->query($sql,array($value)) -> error()){ 

到这一行

if(!$this->query($sql,array($value))->_error{

我收到如下图所示的错误:

error

这不是我之前遇到的错误。

最佳答案

尝试在末尾添加回车符...

public function query($sql,$params =array()){
$this->_error =false;
if($this->_query =$this->_pdo ->prepare($sql)){
if(count($params)){
$x = 1;
foreach($params as $param){
$this->_query->bindValue($x,$param);
$x++;
}
}
if($this ->_query->execute()){
$this->_result=$this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count=$this->_query->rowCount();
}else{
$this->_error =true;
}
}

return $this;
}

关于Phpacademy OOP 登录和注册第 8/23 部分函数 error() 在非对象行 56 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27767846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com