2014-01-23 96 views
3

嗨即時嘗試讓這個特定的代碼工作的類別頁面。不知道如何安排它。PHP,類別,從INNERJOIN選擇在哪裏

 $cat = $_GET['cat']; 
} 
$record_count = $db->query("SELECT * FROM posts WHERE category_id='$cat'"); 
$per_page = 3; 
$pages = ceil($record_count->num_rows/$per_page); 

echo $cat; 

if (isset($_GET['p']) && is_numeric($_GET['p'])){ 
    $page = $_GET['p']; 
}else{ 
    $page = 1; 
} 
if($page<=0) 
    $start=0; 
    else 
    $start = $page * $per_page - $per_page; 
$prev = $page - 1; 
$next = $page + 1; 
$query = $db->prepare("SELECT post_id, title, date, image, LEFT(body, 150) AS body, category FROM posts WHERE posts.category_id=$cat INNER JOIN categories ON categories.category_id=posts.category_id order by post_id desc limit $start, $per_page"); 
$query->execute(); 
$query->bind_result($post_id, $title, $date, $image, $body, $category); 

這包括pagnation,特別是看這條線。但以分離需要的是似乎越硬CATEGORY_ID(至少對我來說)使用兩個MySQL表

帖比預期

$query = $db->prepare("SELECT post_id, title, date, image, LEFT(body, 150) AS body, category FROM posts WHERE posts.category_id=$cat INNER JOIN categories ON categories.category_id=posts.category_id order by post_id desc limit $start, $per_page"); 

:POST_ID標題日期圖像身體鏈路鏈接名稱CATEGORY_ID

類別:類別CATEGORY_ID

+0

它更好地在這裏添加模式,所以我們可以看到表結構。 – Shawn

回答

2

您的查詢片斷不按順序(特別是,哪裏去的地方),而且您沒有正確地執行準備好的語句。請注意,您在每個變量的位置放置一個問號,然後使用bind_param按順序填充問號。第一部分,"iii",意味着有三個整數。

$query = $db->prepare("SELECT post_id, title, date, image, LEFT(body, 150) AS body, category 
    FROM posts 
    INNER JOIN categories ON categories.category_id=posts.category_id 
    WHERE posts.category_id=? 
    order by post_id desc 
    limit ?, ?"); 
$query->bind_param("iii", $cat, $start, $per_page); 
$query->execute(); 
+0

OMG先生是一位學者和傳奇人物謝謝! – user3229639

+0

現在我放棄了它有點像c/matlab編程。乾杯! – user3229639

+1

你的意思是「你太太」:) :) – miyasudokoro