gpt4 book ai didi

PHP PDO : Showing topics out database

转载 作者:行者123 更新时间:2023-11-29 17:51:49 28 4
gpt4 key购买 nike

我需要一些帮助来了解如何在我的网站上显示 mysql (phpmyadmin) 数据库中的所有主题。

This is how it looks like now

其代码是:

<section class="col-md-4 connectedSortable">
<!-- TABLE: LATEST ORDERS -->
<div class="box box-info">
<div class="panel panel-default">
<div class="panel-heading main-color-bg">
<h3 class="panel-title">Topics</h3>
</div>
<div class="container-fluid">
<div class="row content">
<ul class="nav nav-pills nav-stacked">
<li class="active"><a href="#section1">Home</a></li>
<li><a href="#section2">Friends</a></li>
<li><a href="#section3">Family</a></li>
<li><a href="#section3">Photos</a></li>
</ul><br>
</div>
</div>
</div>
</div><!-- /.box -->
</section><!-- right col -->

我希望此列表成为数据库中的主题列表

How the database looks like

这是我的 config.php:

<?php  
date_default_timezone_set('Europe/Amsterdam');

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

$CONFIG = array();
$CONFIG['root'] = '####';
$CONFIG['rootwebsite'] = '####';
$CONFIG['website'] = '####';

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

我尝试过的:

class Forum
{
private $dbh; //dbh = database handler.
public function __construct($database)
{
$this->dbh = $database;
}

/* function that gets the main forum board */
public function getForum()
{
$query = $this->dbh->prepare('SELECT * FROM `topics` ORDER BY `id` ASC');
$query->execute();
$results = $query->fetchAll();

foreach( $results as $row ){
print_r( $row );
}
}
}

最佳答案

这是从 PDO 角度来看的:

<?php
// Connect to DB
try {
$yourdsn = 'mysql:dbname=<YOURDBNAME>;host=<YOURDBHOST>;
$data = new PDO($yourdsn, $youruser, $yourpassword);
$data->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data->exec("set names utf8");
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

然后查询您的表

$sql = "SELECT * FROM topics ORDER BY id DESC";
$topics = $data->prepare($sql);
$topics->execute();
$tops = $topics->fetchAll(PDO::FETCH_OBJ);

最终在 HTML 模板中循环结果

<? foreach($tops as $top):?>
<h1><?=$top->onderwerp?></h1>
<p><?=$top->omschrijving?></p>
<? endforeach;?>

关于PHP PDO : Showing topics out database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49259436/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com