php - 显示 php oop 表中的数据

标签 php mysql oop

我收到一些错误警告来显示数据库中的数据 我在文件夹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 中被删除

mysqli教程:http://www.w3schools.com/php/php_ref_mysqli.asp

关于php - 显示 php oop 表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36707899/

相关文章:

c++ - 为什么在类对象的声明中写一个 "class"?

oop - 限制可能实现 Trait 的类型

c# - 从抽象集合中生成抽象集合

php - array_merge 没有按预期工作

php - 在php中将数组转换为json对象

php - 在不同条件下对单列进行计数

mysql - 如何使用 MySQL 后端在 MS Access 中创建(多字段)搜索表单?

php - 带有 2 个按钮的 Jquery.post 提交表单

使用 Makegood 的 Eclipse 4.2 中的 PHPUnit 测试未运行

php - MySQL 将数据插入数据库时​​影响行 -1