Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

General Help - Resource Error

$thread_id = (int)$_GET['id'];

$query = mysql_query("SELECT * FROM forum_post WHERE id = '$thread_id' AND type = 'a' LIMIT 1");
$numRows = mysql_num_rows($query);

if ($numRows < 1) {
    $errors[] = 'That Thread does not exist. Stop playing with the URL\'s';
    exit();
}

while ($row = mysql_fetch_array($query)) {
    $post_author = $row['post_author'];
    $post_author_id = $row['post_author_id'];
    $date_time = $row['date_time'];
    $date_time = strftime('%b %d, %Y', strtotime($date_time));
    $section_title = $row['section_title'];
    $section_id = $row['section_id'];
    $thread_title = $row['thread_title'];
    $post_body = $row['post_body'];


}

Why does it return resource id# ??

Is this all your code? Unless I'm missing it, I don't see where you are echoing or returning anything for it to give you the resource ID.

As an aside, typically you get a resource ID if you try to echo out the returned value of mysql_query() instead of that of mysql_fetch_array()

5 Answers

I did echo it out because when I was going to use the information from the query well. The information was not showing up so I added the echo on the $query and im getting a resource error

no the code is like 200 lines, wasnt going to post all of it

ok. I just wanted to see what you were trying to echo. But if you say you were trying echo $query, then that's why you are getting that. mysql_query() returns a resource. See the documentation for more info php mysql_query()

How would I echo out the results of the query?

you have it right in your while loop: $row = mysql_fetch_array($query). You just need to echo some of those results were all you are doing right now is assigning results to variables.

so it might look something like this:

while ($row = mysql_fetch_array($query)) {
  echo  $row['post_author'];
  echo $row['post_author_id'];
  echo  $row['date_time'];
echo   strftime('%b %d, %Y', strtotime($date_time));
echo    $row['section_title'];
echo    $row['section_id'];
echo    $row['thread_title'];
echo    $row['post_body'];


}

The problem is they come blank. But the information is in the db

have you tried echoing $_GET['id'] to make sure you are getting something there? or just hardcoding $thread_id?
also, you should surround the table name in your query with backticks

It works correctly

your code works correctly or $_GET['id'] is correct?

if your code overall is still not working, how are you echoing out those variables (i.e $post_body)? what's the result of echo var_dump($row) ?

Im so sorry - That code is the one that works this one isnt

$all_responses = '';
$query = mysql_query("SELECT * FROM forum_post WHERE otid = '$thread_id' AND type = 'b'") or mysql_error();

$numRows = mysql_num_rows($query);

if($numRows < 1) {
    $all_responses = '<div class="alert alert-info text-center" role="alert">No one has respond to this post! You can be the first to post.<br></div>';

}

while ($rows = mysql_fetch_array($query)) {
    $reply_author = $row['post_author'];
    $reply_author_id = $row['post_author_id'];
    $date_n_time = $row['date_time'];
    $converted_time = ago($date_n_time);
    $reply_body = $row['post_body'];
    $all_responses .= '<li class="list-group-item">Re: ' . $thread_title . '&nbsp; &nbsp; &bull; <a href="#">' . $reply_author . '</a> said:' . $reply_body . '</li>';


}


?>

When I do a var dump on the $rows it give me a bool(false)