gpt4 book ai didi

PHP,类别,SELECT FROM INNERJOIN WHERE

转载 作者:可可西里 更新时间:2023-11-01 07:37:14 24 4
gpt4 key购买 nike

您好,我正在尝试让这个特定代码用于类别页面。不确定如何安排。

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

这包括分页,尤其是在查看这一行。但是隔离所需的 category_id 似乎比预期的更难(至少对我而言)

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

使用两个mysql表

posts: post_id title date image body link linkname category_id

类别:类别category_id

最佳答案

您的查询部分顺序不正确(具体来说,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();

关于PHP,类别,SELECT FROM INNERJOIN WHERE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21319545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com