0

I'm working on a project which is using a MySQL database, I had already made this piece of code and it worked before I stopped working at the project like a month ago. So, I'm getting the error: Call to a member function prepare() on null in. I'm aware that there have been a lot of questions like those already, but I can't seem to find an answer.

My database class:

<?php
class Database {

    private $con;

    private $host;
    private $user;
    private $pass;
    private $data;

    public function __construct($host, $user, $pass, $data) {
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->data = $data;
    }

    public function connect() {
        try {
            $this->con = new PDO('mysql:host='.$this->host.';dbname='.$this->data.';charset=utf8', $this->user, $this->pass);
            $this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
            $this->con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            return true;
        } catch (Exception $e) {
            return false;
        }
    }

    public function getRecentPosts($forum_id, $limit) {
        $stmt = $this->con->prepare("SELECT * FROM forums_topics INNER JOIN forums_posts ON forums_topics.tid = forums_posts.topic_id INNER JOIN core_members ON forums_posts.author_id = core_members.member_id WHERE forums_topics.forum_id =$forum_id AND forums_posts.new_topic =1 ORDER BY forums_topics.start_date DESC LIMIT $limit");
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}
?>

Connection and including:

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    require_once 'constants.php';
    require_once 'classes/database.class.php'; 

    $db = new Database($sql_host, $sql_user, $sql_pass, $sql_data);
    $db->connect();
?>

So, for some reason there's something wrong with my SELECT statement, but I can't seem to find what I'm doing wrong.

Thanks in advance!

peer
  • 1,001
  • 4
  • 13
  • 29
  • @Zimmi No, I have called connect() on my page, I'll add that piece of code. – peer Apr 21 '16 at 18:51
  • @JayBlanchard it indeed is. – peer Apr 21 '16 at 18:52
  • @JayBlanchard `$this` is just an instance of the class you're currently in. – peer Apr 21 '16 at 18:55
  • @JayBlanchard it's reffering to the `private $con;` at the top. – peer Apr 21 '16 at 18:58
  • @JayBlanchard As it has worked before, yes I'm positive. – peer Apr 21 '16 at 19:06
  • `connect()` returns true or false, but it doesn't look like you're checking the return value when you use it. – Don't Panic Apr 21 '16 at 19:07
  • The problem with this code is that PDO cannot connect but you don't know that due to wrong error reporting. Refer to the linked answer for a proper one. – Your Common Sense Apr 21 '16 at 19:11
  • Instead of returning `false` in your `catch{}`, why don't you return the `Exception` so you can see what's going on? And returning *true* in your `try{}` is redundant as you shouldn't be checking whether your `connect()` method is of type `bool`, but instead, whether it's holding the `PDO` object or not. – mferly Apr 21 '16 at 19:18

0 Answers0