-4

I want to show all the records from the DB on my website, it's a PDO built website. I want it to show all the records so you can see what's in the DB.

This is how my DB looks like

The connection is set up in a different document called config.php

<?php

date_default_timezone_set('Europe/Amsterdam');

error_reporting(E_ALL & ~ E_DEPRECATED);
ini_set('display_errors', 'ON');

$CONFIG = array();
$CONFIG['root'] = '/home/tom/public_html';
$CONFIG['rootwebsite'] = '/home/tom/public_html';
$CONFIG['website'] = 'https://###';

$CONFIG['dbhost'] = 'localhost';
$CONFIG['dbuser'] = '####';
$CONFIG['dbpass'] = '####';
$CONFIG['dbdatabase'] = 'tom';
?>

This is the code I have in a php document and tried using. The problem is it won't show anything on my website (this is a different file than the file my website is):

<?php

class Forum {

    private $dbh; //dbh = database handler.

    public function __construct($database) {
        $this->dbh = $database;
    }

    public function getForum() {
        $getTopic = $dbh->prepare("SELECT * FROM topics ORDER BY id DESC");
        $getTopic->execute();
        $topics = $getUTopic->fetchAll();
        foreach ($topics as $topic) {
            echo $topic['onderwerp'] . '<br />';
        }
    }

}
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

You are not calling the right $connection variable. It should be $this->dbh

$getTopic = $this->dbh->prepare("SELECT * FROM topics ORDER BY id DESC");

Also you are mixing the variables after execute(). You should use easy to remember variables.

public function getForum() {
    try {
        $getTopic = $this->dbh->prepare("SELECT * FROM topics ORDER BY id DESC");
        $getTopic->execute();
        $topics = $getTopic->fetchAll();
        foreach ($topics as $topic) {
            echo $topic['onderwerp'] . '<br />';
        }
    } catch (PDOException $pdoEx) {
        echo $pdoEx->getMessage();
        exit;
    } catch (Exception $ex) {
        echo $ex->getMessage();
        exit;
    }
}

Also since you are not passing any variable to your query, there is no point of using prepare(). Just call query() instead.

For error reporting add,

error_reporting(E_ALL); #place at the top of the script

Also you should consider using a proper IDE like PHPstorm or Netbeans, as they would easily point out unsed variables

As suggested by @adpro, here is a link, to help with debugging

Rotimi
  • 4,783
  • 4
  • 18
  • 27
  • Plus one for the IDE suggestion - is there a general debugging php topic on here? That would be nice to link to for questions like this. – adprocas Mar 22 '18 at 13:06
  • If I change it like this it still wont show any errors or records on the website. – Tomishotttieee Mar 22 '18 at 13:07
  • This article is linked in the help - [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – adprocas Mar 22 '18 at 13:09
  • 1
    @Tomishotttieee how do you establish PDO connection? also how do you call the function `getForum()`? Please add it to your question – Rotimi Mar 22 '18 at 13:12
  • The OP first needs to get [error reporting working](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – adprocas Mar 22 '18 at 13:12
  • I am not calling the function `getForum`?and you can see my config file? or do you mean this? `$database = new Database($CONFIG['dbuser'] ,$CONFIG['dbpass'] ,$CONFIG['dbdatabase'], $CONFIG['dbhost']);` – Tomishotttieee Mar 22 '18 at 13:15
  • you have to call the function `getForum()` ! then why have it in the first place? – Rotimi Mar 22 '18 at 13:16
  • @Akintunde007 how do i do that in this case? – Tomishotttieee Mar 22 '18 at 14:41