按特定类别从数据库中获取博客
getting blogs from database by specific category
我创建了一个包含类别的博客系统我在数据库中有两个 table,table 个博客 blog_id
,title
,body
, category_id
和第二个 table 列 category
、category_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");
在 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");
和确保在您的table
中有一个table字段调用category
我创建了一个包含类别的博客系统我在数据库中有两个 table,table 个博客 blog_id
,title
,body
, category_id
和第二个 table 列 category
、category_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");
在 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");
和确保在您的table
中有一个table字段调用category