1

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
  • What database abstraction layer are you using? Commented Sep 1, 2011 at 0:50
  • 1
    What level is error_reporting set to? Add this code to the top of the page: error_reporting(E_ALL & ~E_STRICT); and tell us what errors you see, if any. Commented Sep 1, 2011 at 1:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I am using MySqli. I was under the impression that result binding was optional, but it turns out it isn't! This solved the problem. Thanks!
0

try a

var_dump($all_comments) 

after you fetch it, to prove that there is actually something in the array

next step would be to check that the sql worked. I am not sure what database layer you are using so i'm not sure what the check to do that would be.

i would assume that this method should have a return value you can check

$stmt->execute();

1 Comment

I tried var_dump and it showed that all the object values in the array are NULL, so that explains why nothing is being displayed. Doesn't really solve the problem though, I don't know what to try next. I'm fairly new to PHP (I'm used to C#), but I assume by database layer you mean some kind of ORM layer? I'm not using any at the moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.