PHP, categories, CHOOSE FROM INTERNAL WHERE

hi im is trying to get this particular code to work on the category page. not sure how to arrange it.

        $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 );

This includes pagnation and, in particular, considers this line. but to highlight the desired extraction_ category seems to be harder (at least for me) than expected

$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");

using two mysql tables

Posts: post_id name date image body link linkname category_id

categories: category category_id

+4
source share
1 answer

( , WHERE), . , , , 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();
+2

Source: https://habr.com/ru/post/1523426/


All Articles