[英]getting blogs from database by specific category
我創建了一個帶有類別的博客系統,我在數據庫中有兩個表,帶有blog_id
, title
, body
, category_id
表 blogs 和帶有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");
並確保您的表格中有一個表格字段調用category
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.