Page 1 of 1

Blog Unknown column 'id'

Posted: Wed May 25, 2011 7:58 pm
by JelvinJS7
#fetches a summary of all blog posts
function get_posts(){
    $sql = "SELECT
            `posts`.`id` AS `id`,
            `posts`.`title` AS `title`,
            LEFT(`posts`.`body`, 512) AS `preview`,
            `posts`.`user` AS `user`,
            DATE_FORMAT(`post`.`date`, '%d/%m/%Y %H:%i%s') AS `date`,
            `comments`.`total_comments`,
            DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i%s') AS `last_comment`
        FROM `posts`
        LEFT JOIN (
            SELECT
                `id`,
                COUNT(`id`) AS `total_comments`,
                MAX(`date`) AS `last_comment`
            FROM `comments`
            GROUP BY `id`
        ) AS `comments`
        ON `posts`.`id` = `comments`.`id`
        ORDER BY `posts`.`date` DESC";
    
    $posts = mysql_query($sql) or die(mysql_error());
    $rows = array();
    while (($row = mysql_fetch_assoc($posts)) !== false){
        $rows[] = array(
            'id'             => $row['id'],
            'title'          => $row['title'],
            'preview'        => $row['preview'],
            'user'           => $row['user'],
            'date'           => $row['date'],
            'total_comments' => ($row['total']===null) ? 0 : $row['total_comments'],
            'last_comment'   => ($row['last_comment'] === null ? 'never' : $row['last_comment'])
        );
        
    }
    
    return $rows;
}

# fetches a single post from the table
function get_post($pid){
    $pid = (int)$pid;
    
    $sql = "SELECT
        `title` AS `title`,
        `body` as `body`,
        `user` as `user`,
        `date` as `date`,
    FROM `posts` WHERE `posts`.`id` = {$pid}";
    $post = mysql_query($sql);
    $post = mysql_fetch_assoc($post) or die(mysql_error());
    $post['comments'] = get_comments($pid);
    
    return $post;
}
blog_list.php:
<?php
include("core/init.inc.php");
?>
<html>
    <body>
        <div>
            <?php
            
            $posts = get_posts();
            
            foreach($posts as $post){
                ?>
                
                <h1><?php echo $post['title']; ?></h1>
                    <?php echo "<h3>By ".$post['user']." on ".$post['date']; ?>
                
                
                <h4><?php echo $post['total_comments']; ?> comment(s); last one on <?php echo $post['last_comment']; ?></h4>
                <hr />
                <h5>Post:</h5>
                <p><?php echo $post['preview']; ?></p>
                
                <?php
            }
            
            ?>
        </div>
    </body>
</html>
mysql_error wrote:Unknown column 'id' in 'field list'
for my table, i dropped the "post_" prefix.

Please help! I can't find the problem!

Re: Blog Unknown column 'id'

Posted: Wed May 25, 2011 8:00 pm
by jacek
First guess... do you have a column called "id" ? ;)

Re: Blog Unknown column 'id'

Posted: Wed May 25, 2011 8:17 pm
by JelvinJS7
jacek wrote:First guess... do you have a column called "id" ? ;)
(you changed the name, didn't you?)
yes. yes i do

Re: Blog Unknown column 'id'

Posted: Wed May 25, 2011 8:33 pm
by Tino
Second guess... Do you have an id field in both the posts and the comments tables?

Re: Blog Unknown column 'id'

Posted: Wed May 25, 2011 8:45 pm
by JelvinJS7
Tino wrote:Second guess... Do you have an id field in both the posts and the comments tables?
comments table columns:
comment_id
post_id
comment_user
comment
date

Posts table columns:
id
title
user
post
date

Re: Blog Unknown column 'id'

Posted: Wed May 25, 2011 8:55 pm
by Tino
Well there's your problem. In the nested select query in that large one, you're selecting id from comments. That should be comment_id.

Re: Blog Unknown column 'id'

Posted: Wed May 25, 2011 9:16 pm
by JelvinJS7
yup. that was basically it
basically forgot to add "comment_" to the id part of the comments, plus had some misspellings.

it'd be nice if the error was a bit more specific though…

Re: Blog Unknown column 'id'

Posted: Wed May 25, 2011 9:29 pm
by jacek
JelvinJS7 wrote:it'd be nice if the error was a bit more specific though…
How could is possibly be more specific ?

Re: Blog Unknown column 'id'

Posted: Wed May 25, 2011 9:37 pm
by JelvinJS7
jacek wrote:
JelvinJS7 wrote:it'd be nice if the error was a bit more specific though…
How could is possibly be more specific ?
error wrote:Unknown column name 'id' in 'field set'
they could say which table, and list similar column names that exist that could be what we mean