按特定类别从数据库获取博客

时间:2015-09-08 03:58:14

标签: php mysql blogs categories

我创建了一个包含类别的博客系统,我在数据库中有两个表,包含blog_idtitlebodycategory_id的表博客和第二个包含列的表categorycategory_id。要从所有类别获取博客我使用此代码,它运作良好。

  $query = ("SELECT blogs_id, title, body, posted_by, category FROM blogs  INNER JOIN categories ON categories.category_id=blogs.category_id  ORDER BY  blogs_id desc LIMIT 10");
 $result = mysql_query($query);
 $result = mysql_query($query) or die("error:".mysql_error());
 while ($row = mysql_fetch_assoc($result)) {
        $title = $row['title'];
        $body = $row['body']; 
        $posted_by = $row['posted_by'];

现在我想按特定类别获取博客,我使用相同的查询添加" where category=that category"它没有用,所以我尝试了category_id,但它也失败了。我的代码是这样的

        $query = ("SELECT blogs_id, title, body, posted_by, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.category_id where category=anycategory ORDER BY blogs_id desc LIMIT 10");

1 个答案:

答案 0 :(得分:1)

'子句上使用where

$query = ("SELECT blogs_id, title, body, posted_by, category 
FROM blogs INNER
JOIN categories ON categories.category_id=blogs.category_id 
where category= 'anycategory'
ORDER BY blogs_id desc LIMIT 10");

确保您的表格中有一个表格字段致电category