duan4739 2018-03-22 12:54
浏览 67

PHP Pdo:显示数据库中的记录[重复]

This question already has an answer here:

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 />';
        }
    }

}
</div>
  • 写回答

1条回答 默认 最新

  • dsjpqpdm620596 2018-03-22 13:00
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错