코드에 실제로 잘못된 것이 무엇인지 이해하기 위해 모든 노력을 기울였습니다. 그러나이 오류의 원인을 파악할 수 없었습니다. 다음은 null의 멤버 함수를 호출합니까?
는 클래스Topic
라고 :
치명적인 오류 :
class Topic { // Init DB variables private $db; /* * Constructor */ public function __contruct() { $this->db = new Database(); } /* * Get All Topics */ public function getAllTopics() { $this->db->query("SELECT topics.*, users.username, users.avatar, categories.name FROM topics INNER JOIN users ON topics.user_id = users.id INNER JOIN categories ON topics.category_id = categories.id ORDER BY create_date DESC"); // Assign the results $results = $this->db->resultset(); return $results; } }
내가 클래스
Topic
의 객체를 생성,$topic = new Topic(); $results = $topic->getAllTopics();
항상이 같은 오류가 : 멤버 함수를 호출합니다. query() o
Database
클래스는 완벽하게 작동되는 라인에 /Applications/XAMPP/xamppfiles/htdocs/forumproject/libraries/Topic.php 에서 n은 널 (null) 25 예는 $ this-> db-> query (...) 그 클래스에 대해 독립적으로 테스트를 실행 했으므로 아무런 문제가 없습니다. 여기
<?php
/*
* Using PDO for the first time in my life.
*/
class Database {
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $error;
private $stmt;
public function __construct() {
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instance
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch the errors using exceptions handler
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if(isnull($type)){
switch(true){
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default :
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_OBJ);
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->bbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
}
?>
'Database' 클래스가 같은 파일에 있습니까? 그렇지 않다면'include '/'require'해야합니다. –
전체 주제 파일입니까? 치명적인 오류가 25 번 줄에서 제안됩니다. 여기에서 쿼리 기능이 no no no line에서 호출됩니다. 19 –
@TomUdding 예 포함했습니다. – manjula