我收到一些错误警告来显示数据库中的数据 我在文件夹classes/Comment.php中有代码
<?php
class Comment
{
protected $database;
public function fetch($table, $rows = '*', $where = null, $order = null)
{
$result = "SELECT {$rows} FROM {$table}";
if ($where != null) {
$result += " WHERE {$where}";
}
if ($order != null) {
$result += " ORDER BY {$order}";
}
return $result;
}
在我的index.php中我写道:
<?php
$comment = new Comment();
$results = $comment->fetch('', '$tableName', 'id_comment DESC');
$comments = array();
while ($comment = mysql_fetch_array($results)) {
$comments[] = $comment;
}
?>
<?php foreach ($comments as $comment) : ?>
<div class="box-comment">
<div class="content">
<?php echo h($comment['content']) ?>
</div>
<div class="date-time">
<?php echo $comment['posted_at'] ?>
</div>
</div>
<?php endforeach ?>
但是我的代码出现错误,请帮助我
最佳答案
首先尝试执行查询。我猜$results返回一个sql查询字符串,所以我建议你使用这个:
$result = mysql_query($results);
然后您可以使用以下方式获取数据:
while ($comment = mysql_fetch_array($results)) { $comments[] = $comment; }
引用:http://php.net/manual/en/function.mysql-fetch-array.php
我给你的建议是学习 mysqli,因为 mysql 在 PHP 5.5.0 中已被弃用,并且在 PHP 7.0.0 中被删除
关于php - 显示 php oop 表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36707899/