0

我有两个表格,帖子和部分。我想获得最后 10 个帖子 WHERE section = 1,但在不同的地方使用 10 个结果。我做了一个功能:

     function sectionposts($section_id){
mysql_set_charset('utf8');
$maxpost1 ="SELECT max(id) from posts WHERE section_id = $section_id ORDER BY ID DESC LIMIT 20";
$maxpost12 =mysql_query($maxpost1);
while ($maxpost_rows = mysql_fetch_array($maxpost12 ,MYSQL_BOTH)){
$maxpost2 = $maxpost_rows[0];
}
$query = "SELECT * FROM posts WHERE id = $maxpost2";
$query2 = mysql_query($query);
return $query2;
}
$query2 = sectionposts(6);
while ($rows = mysql_fetch_array($query2)){
echo $rows['title'] . "<br/>" . "<br/>";
echo $rows['id'] . "<br/>" . "<br/>";
echo $rows['image_section'] . "<br/>";
echo $rows['subject'] . "<br/>";
echo $rows['image_post'] . "<br/>";
}

怎么能把这十个结果用在不同的地方,从一到十排列。

这是旧案例,我解决了,但我发现了另一个问题,如果客户删除了一个 id = 800 的帖子“所以 DB 中没有 id = 800”,所以当我得到最大 id 减去 $NUM从中,这个操作必须等于 id = 800,所以我这里有一个编程错误,我该如何处理这样的事情。

    function getmax_id_with_minus ($minus){
    mysql_set_charset('utf8');
    $maxid ="SELECT max(id) FROM posts";
    $maxid1 =mysql_query($maxid);
        while ($maxid_row = mysql_fetch_array($maxid1)){
                $maxid_id = $maxid_row['0'];
                $maxid_minus = $maxid_id - $minus;
                }
    $selectedpost1 = "SELECT * FROM posts WHERE id = $maxid_minus";
    $query_selectedpost =mysql_query($selectedpost1);
        return ($query_selectedpost);

}
<?php 
$ss = getmax_id_with_minus (8);
while ($rows = mysql_fetch_assoc($ss)){
$main_post_1 = $rows;
?>

无论如何“真的”再次感谢:)!

4

3 回答 3

0

试试这个,为自己节省很多无意义的代码:

$sql = "SELECT * FROM posts WHERE section_id=$section_id HAVING bar=MAX(bar);"
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo ...;
echo ...;

having子句使您可以在单个操作中找到最大记录,而没有两个查询版本的固有竞争性。除非您允许具有相同 ID 的多条记录污染您的表,否则删除 while() 循环也会使事情变得更加清晰。

于 2012-09-09T02:32:19.243 回答
0

似乎您想将它们存储在数组中。

 $posts = array(); //put this before the while loop. 
 $posts[] = $row; //put this in the while loop
于 2012-09-09T01:16:32.957 回答
0

关于发布代码的一些想法:

  • 首先,您应该停止使用mysql_函数,因为它们已被弃用并且容易受到 SQL 注入的攻击。
$maxpost1 ="SELECT max(id) from posts WHERE section_id = $section_id ORDER BY ID DESC LIMIT 20";
  • 当您SELECT MAX, MIN, COUNT, AVG... 只返回单行的函数时,您不需要 anORDER BY或 a LIMIT

  • 鉴于您只要求MAX(id),您可以通过组合查询来节省工作,如下所示:

SELECT * FROM posts 
WHERE id = (SELECT MAX(id) from posts WHERE section_id = $section_id)

如果我理解你想要做什么(如果我错了,请纠正我),你的函数看起来像:

function sectionposts($section_id) {
    $link = mysqli_connect("localhost", "my_user", "my_password", "world");

    $stmt = mysqli_prepare($link, "SELECT title, id, image_section, subject, image_post FROM posts " 
        . "WHERE section_id = ? ORDER BY id DESC LIMIT 10");
    mysqli_stmt_bind_param($stmt, $section_id);
    return mysqli_query($link, $stmt)
}

$result = sectionposts(6);
while ($row = mysqli_fetch_assoc($result)) {
    echo $rows['title'] . "<br /><br />";
    echo $rows['id'] . "<br /><br />";
    echo $rows['image_section'] . "<br />";
    echo $rows['subject'] . "<br />";
    echo $rows['image_post'] . "<br />";
}
于 2012-09-09T02:45:17.250 回答