0

I've created a function called tree() which is supposed to retrieve data from my comments table and display comments in a thread in a hierarchy, much like the comments on reddit. The $parentid is the id of the parent comment and the $postid is the id of the page the comment is on. DisplayComments() is a php function which displays the comment's data and forms, including html.

Now the issue is when I load the page the php code stops at $statement = $databaseConnection->prepare($query); when it's done inside of the function. However, if I take the code outside of the function the code works, but I can't use the code inside of DisplayComments() and can only show the comment replies as if they were the start of new threads.

Why can't I use this mysql query inside of a function? Is there anyway I can use it inside of a function so I can call it where I need it?

function tree($parentid, $postid) {
    $query = "SELECT * FROM comments WHERE parentid = ?";
    $statement = $databaseConnection->prepare($query);
    $statement->bind_param('i', $parentid);
    $statement->execute();
    $statement->store_result();
    if ($statement->error)
    {
        die('Database query failed: ' . $statement->error);
    }

    if ($statement->num_rows >= 1)
    {
        $statement->bind_result($commentid, $postid, $username, $comment, $parentid, $level);
        while($statement->fetch()){
            if (!is_hidden($commentid, 1)){ 
                DisplayComments($comment, $username, $postid, $commentid, $level);
            }   
        }
    }
}
1
  • If you want to learn how to write a good code, learn about Data Mapper / Repository pattern. Commented Mar 7, 2015 at 14:54

1 Answer 1

1

Your tree function has no knowledge of $databaseConnection. You need to pass it as another variable, e.g.

function tree($parentid, $postid, $databaseConnection) {

Have a read of the PHP manual's page on variable scope (but try not to use global, that's rarely a good thing)

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

5 Comments

It partially works. It displays one nested comment but now that comment has the previous issue. I should mention that DisplayComment() calls tree() inside of itself too so they loop around another.
This line $statement->bind_result($commentid, $postid, $username, $comment, $parentid, $level); contains more variables that are out of scope. You're going to need to pass those too
The bind-result is me passing the selected data from the database into variables. How are these out of scope?
No problem. Is there anything else you can see which I could fix?
Nevermind. I fixed it! Instead of doing function tree($parentid, $postid, $databaseConnection) I just created a global variable and placed it in the function. Thanks for your help though!

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.