I try to code a project using PDO class and it fails on error
"Call to a member function prepare() on a non-object"
I found some answers on stackoverflow but they didnt work and I dont know what to do now. I started try fix this 2 days ago and I still can not fix this. Please, if you know where is the problem, help me.
My config file:
//DB Params
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "usbw");
define("DB_NAME", "talkingspace");
//define("SITE_TITLE", "Welcome to Talking Space");
//Paths
define('BASE_URI', 'http://'.$_SERVER['SERVER_NAME'].'/MVC/');
?>
And my PDO file
<?php 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 (Exception $e) {
$this->error = $e->getMessage();
}
}
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type=null) {
if (is_null($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->dbh->commit();
}
public function cancelTransaction() {
return $this->dbh->rollBack();
}
}
it fails on error "Call to a member function prepare() on a non-object"
That's because you're calling the single()
or resultset()
method before the query()
method. The correct order of statements should be like this:
$db = new Database();
$db->query("SELECT * FROM table_name");
$result = $db->single(); // or $result = $db->resultset();
var_dump($result); // to see the result set
Edited:
name
is reserved keyword in MySQL. You should escape it using backticks. Always use backticks to wrap the table and column names and use quotes to wrap your strings. Backticks are only necessary when the table name or column name is a MySQL reserved keyword.
Your code should be like this: (tested
)
class Topic {
private $db;
public function __construct() {
$this->db = new Database;
}
public function getAllTopics() {
$this->db->query("SELECT `name` FROM `users` order by `name` asc");
$results = $this->db->resultset();
return $results;
}
}
$obj = new Topic();
var_dump($obj->getAllTopics()); // to see the result set