duannaiying9662 2011-09-01 00:24
浏览 148
已采纳

PHP不显示数据库中的数据

I am trying to display a list of comments from a MySql database in PHP.

The foreach loop works as it displays the necessary html for each comment in the database, but no actual content from the database is being pulled through.

Comment class

class Comment {
protected $_id;
protected $_user;
protected $_commentText;
protected $_dateTimePosted;

public function __construct()
{
    $this->_dateTimePosted = new DateTime();
    $this->_dateTimePosted->format(DATE_RFC3339);
}

public function get_id()
{
    return $this->_id;
}
public function set_id($value)
{
    $this->_id = $value;
}
public function get_user()
{
    return $this->_user;
}
public function set_user($value)
{
    $this->_user = $value;
}
public function get_commentText()
{
    return $this->_commentText;
}
public function set_commentText($value)
{
    $this->_commentText = $value;
}
public function get_dateTimePosted()
{
    return $this->_dateTimePosted;
}
public function set_dateTimePosted($value)
{
    $this->_dateTimePosted = $value;
}
}

CommentFunctions.php

include 'dbConnect.php';

class CommentFunctions {

protected $conn;

public function __construct()
{
    $this->conn = dbConnect();
}
public function get_comments()
{
    $sql = "SELECT * FROM comments";

    $stmt = $this->conn->stmt_init();

    $stmt->prepare($sql);

    $stmt->execute();

    $stmt->store_result();

    $comments = array();

    while ($row = $stmt->fetch())
    {
        $comment = new Comment();

        $comment->set_id($row['id']);
        $comment->set_user($row['user']);
        $comment->set_commentText($row['comment_text']);
        $comment->set_dateTimePosted($row['datetime_posted']);

        $comments[] = $comment;
    }

    return $comments;
}
}

Index.php

<?php
include './includes/Comment.php';
include './includes/CommentFunctions.php';

$comments_func = new CommentFunctions();

$all_comments = $comments_func->get_comments();

?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Comments</title>
        <link rel="stylesheet" type="text/css" href="./css/Master.css" />
        <link rel="stylesheet" type="text/css" href="./css/Site.css" />
    </head>
    <body>
        <div id="Container">
        <h2>Comments</h2>
        <a class="reload-comments" href="/">Refresh</a>
        <div id="Comments">
            <?php if (!$all_comments) {
                echo 'No comments yet.';
            } ?>
            <?php foreach ($all_comments as $c) { ?>
            <div class="comment">
                <input class="id" type="hidden" value="<?php echo $c->get_id(); ?>" />
                <div class="author">Posted by <?php echo $c->get_user(); ?></div>
                <div class="comment-text">
                    Posted <?php echo $c->get_dateTimePosted(); ?>
                    <p><?php echo $c->get_commentText(); ?></p>
                </div>
            </div>
            <?php } ?>
        </div>
        <div id="AddComment">
            <form name="add_comment_form" id="add_comment_form" action="index.php" method="post">
                <label for="user">Your Name:</label>
                <input name="user" id="user" type="text" /><br />
                <label for="comment_text">Comment:</label>
                <textarea name="comment_text" id="comment_text" rows="5" cols="10"></textarea><br />
                <input name="submit" id="submit" type="submit" value="Submit" />
                <input id="reset" type="reset" class="hidden" />
            </form>
        </div>
        <div class="loader"></div>
        <div class="response"></div>
    </div>
</body>

Comments can be added, the data is stored fine in the database, and the loop runs the correct number of times, but the code such as echo $c->get_commentText(); is not displaying a value.

Appreciate any help.

  • 写回答

2条回答 默认 最新

  • dongzai5181 2011-09-01 02:05
    关注

    Looks like you're using mysqli.

    You're forgetting a key step: binding your result variables.

    See http://www.php.net/manual/en/mysqli-stmt.bind-result.php and the examples there for more info on how to get actual values back.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条