Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7811431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:09:27+00:00 2026-06-02T04:09:27+00:00

This function doesn’t work and I can’t find the problem, which is somewhere inside

  • 0

This function doesn’t work and I can’t find the problem, which is somewhere inside the query.

function get_category_posts($category, $page, $per_page) {
$start = (int)($page - 1) * $per_page;
$per_page = (int)$per_page;

$sql = "SELECT
    `posts`.`post_id` AS `id`,
    `posts`.`post_title` AS `title`,
    `posts`.`post_category` AS `category`,
    `posts`.`post_body` AS `preview`,
    `posts`.`post_user` AS `user`,
    DATE_FORMAT(`posts`.`post_date`, '%Y-%m-%d %H:%i:%s') AS `date`,
    `comments`.`total_comments`,
    DATE_FORMAT(`comments`.`last_comment`, '%Y-%m-%d %H:%i:%s') AS `last_comment`
FROM `posts`
WHERE `category`='".$category."'
LEFT JOIN (
    SELECT
        `post_id`,
        COUNT(`comment_id`) AS `total_comments`,
        MAX(`comment_date`) AS `last_comment`
    FROM `comments`
    GROUP BY `post_id`
) AS `comments`
ON `posts`.`post_id` = `comments`.`post_id`
ORDER BY `posts`.`post_date` DESC
LIMIT {$start}, {$per_page}";

$posts = mysql_query($sql);
//die(mysql_error());

$rows = array();
while (($row = mysql_fetch_assoc($posts)) !== false) {
    $rows[] = array(
        'id' => $row['id'],
        'title' => html_entity_decode($row['title']),
        'category' => html_entity_decode($row['category']),
        'preview' => html_entity_decode($row['preview']),
        'user' => html_entity_decode($row['user']),
        'date' => $row['date'],
        'total_comments' => ($row['total_comments'] === null) ? 0 : $row['total_comments'],
        'last_comment' => ($row['last_comment'] === null) ? 'aldrig' : $row['last_comment']
        );
}

return $rows;

}
The function works fine without the WHEREcategory='".$category."', however, with it it returns

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘LEFT JOIN ( SELECT post_id, COUNT(comment_id) AS total_comments, ‘ at line 12

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:09:30+00:00Added an answer on June 2, 2026 at 4:09 am

    The WHERE clause must come after the table joins and before the ORDER BY and LIMIT

    SELECT <column list>
    FROM <table>
    JOIN <table> USING (<column>)
    WHERE <condition>
    OR <condition>
    AND <condition>
    GROUP BY <column list>
    HAVING <expression>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created this example The remove function doesn't work, and I can't figure out
I can't seem to find why this function doesn't insert records into the database.
This doesn't seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m)
Why doesn't this work right? function test() { var start = new Date(2012, 3,
Why doesn't this code work in IE6 or IE7? $('a').click( function() { var urlIsExternal
This function doesn't work: function remove_ul($ul) { $ul = preg_replace('/<ul id=general-nav>/', '<ul class=nav>', $ul,
Any idea why this function doesn't work properly in Internet Explorer? function days_between(check_in, check_out)
I really don't understand why this function doesn't work: function GetNomRepertoireTemporaire:WideString; var PathLocal :
I didn't realize this, but apparently Python's strftime function doesn't support dates before 1900:
Ideally I want a function something like the following (this example doesn't compile): void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.