이 함수는 작동하지 않으며 쿼리 내의 어딘가에 문제를 찾을 수 없습니다. PHP와 MySQL,이 함수의 문제점은 무엇입니까?
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;
} 기능은 그것과는
당신은 당신의 SQL 구문에 오류가 반환하지만,
WHERE
카테고리='".$category."'
없이 잘 작동; 사용할 수있는 권리 구문에 대한 MySQL 서버 버전에 해당하는 설명서를 확인 근처 줄에서 'LEFT JOIN (total_comments
ASpost_id
, COUNT (comment_id
)을 선택'테이블 조인 후 절이 와야 (12)
내가 가입 후 넣어 경우, 나는이 얻을 WHERE 전에 올 필요 조인 절 " ' –
thats는 카테고리가 테이블에 없기 때문에 - 단지 컬럼 별칭 일 뿐이므로 대신'posts.post_category'를 사용하십시오. – Mikey
이제 작동합니다. 감사합니다. :) –