Reputation: 3
I have error in my code: Fatal error: Call to a member function query() on a non-object in /var/www/crud_php/core/class_ManageDatabase.php on line 23
Error line: $query = $this->link->query("SELECT * FROM $table_name ORDER BY id ASC");
<?php
class ManageDb{
public $link;
function __construct() {
include_once 'class_database.php';
$conn = new database;
$this->link = $conn->connect();
return $this->link;
}
function getData($table_name, $id=null){
if(isset($id)){
$query = $this->link->query("SELECT * FROM $table_name WHERE id = '$id' ORDER BY id ASC");
}else{
**$query = $this->link->query("SELECT * FROM $table_name ORDER BY id ASC");**
}
$rowCount = $query->rowCount();
if ($rowCount >=1){
$result = $query->fetchAll();
}else{
$result = 0;
}
return $result;
}
}
?>
db connect:
<?php
include_once '../config.php';
class database {
protected $db_conn;
public $db_name = DB_NAME;
public $db_host = DB_HOST;
public $db_pass = DB_PASS;
public $db_user = DB_USER;
function connect() {
try {
$this->db_conn = new PDO("mysql:host = $this->db_host;dbname=$this->db_name", $this->db_user, $this->db_pass);
} catch (PDOException $e) {
return $e->getMessage();
}
}
}
?>
Upvotes: 0
Views: 386
Reputation: 318508
It means $this->link
is not an object. You forgot to return the connection in your connect()
method. Add return $this->db_conn;
in that method.
On a side-note, returning a string in case of an error is a very bad idea. Let the exception propagate or terminate the script - but not return something completely else that will cause odd errors later in your code. You also cannot return anything from a constructor.
Upvotes: 1