I have error in my code:
Fatal error: Call to a member function query() on a non-object in ... on line 47
Line 47:
$query = $this->link->query("SELECT * FROM links ORDER BY DESC");
Full Code:
<?php
class Manage{
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");
}else{
$query = $this->link->query("SELECT * FROM $table_name ORDER BY id ASC");
}
$rowCount = $query->rowCount();
if($rowCount >=1)
{
$query->setFetchMode(PDO::FETCH_ASSOC);
$result = $query->fetchAll();
}
else{
$result = 0;
}
return $result;
}
function insertData($table_name, $fields_name, $bind_param, $field_values){
$query = $this->link->prepare("INSERT INTO $table_name ($fields_name) VALUES ($bind_param)");
$query->execute($field_values);
$rowCount = $query->rowCount();
return $rowCount();
}
function deleteData($table_name, $id){
$query = $this->link->query("DELETE FROM $table_name WHERE id = '$id'");
$rowCount = $query->rowCount();
return $rowCount;
}
function selectLinks(){
$query = $this->link->query("SELECT * FROM links ORDER BY DESC");
$rowCount = $query->rowCount();
if($rowCount >=1)
{
$query->setFetchMode(PDO::FETCH_ASSOC);
$result = $query->fetchAll();
}
else{
$result = 0;
}
return $result;
}
}
How fix it?
The error about "non-object" is your problem here...the database object was not created to begin with, so subsequent actions with that non existent object will fail. Your additional info about authorization is what you have to fix...properly authorize and connect, then the database object will be created, thus exist when you use it.