【问题标题】:PHP not displaying data from databasePHP不显示数据库中的数据
【发布时间】:2011-09-01 00:24:32
【问题描述】:

我正在尝试在 PHP 中显示来自 MySql 数据库的 cmets 列表。

foreach 循环的工作原理是为数据库中的每条评论显示必要的 html,但没有从数据库中提取任何实际内容。

评论类

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

索引.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>

可以添加评论,数据在数据库中保存完好,循环运行次数正确,但代码如 echo $c->get_commentText();没有显示值。

感谢任何帮助。

【问题讨论】:

  • 你使用的是什么数据库抽象层?
  • error_reporting 设置为什么级别?将此代码添加到页面顶部:error_reporting(E_ALL &amp; ~E_STRICT); 并告诉我们您看到的错误(如果有)。

标签: php mysql


【解决方案1】:

看起来你正在使用 mysqli。

您忘记了一个关键步骤:绑定结果变量。

请参阅http://www.php.net/manual/en/mysqli-stmt.bind-result.php 和那里的示例,了解有关如何取回实际值的更多信息。

【讨论】:

  • 是的,我正在使用 MySqli。我的印象是结果绑定是可选的,但事实证明不是!这解决了问题。谢谢!
【解决方案2】:

试一试

var_dump($all_comments) 

你拿到它之后,证明数组里确实有东西

下一步是检查 sql 是否有效。我不确定您使用的是哪个数据库层,所以我不确定要执行的检查是什么。

我会假设这个方法应该有一个你可以检查的返回值

$stmt->执行();

【讨论】:

  • 我尝试了 var_dump,它显示数组中的所有对象值都是 NULL,这就解释了为什么什么都没有显示。虽然并没有真正解决问题,但我不知道下一步该尝试什么。我对 PHP 相当陌生(我习惯于 C#),但我假设数据库层是指某种 ORM 层?我暂时没有使用任何东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2017-06-01
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 2017-10-17
相关资源
最近更新 更多