从1 - 7

时间:2017-12-27 08:08:40

标签: php mysql

您好我一直在尝试理解选择和回显表中的有限行数,但我不明白如何在代码中实现它,所以我希望有人可以提供帮助

我有这个代码,我首先要在循环中回显,只有列id为1-7(这也是第1-7行)我该怎么做 我知道这应该是这样做的方法:

SELECT * FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY ID) NUM,
* FROM bangpakong
) 
WHERE NUM >0 AND NUM <7

但是上面只是给我一个语法错误,当我在代码中使用它时,以下是代码工作,而不仅仅是选择1-7之间的行

$sql = "SELECT * FROM bangpakong";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<div class='boks'>";
    echo "<div id='courseC'>";
echo "<img src='http://pattayasports.org/calendar/wp-
content/uploads/2017/11/Bangpakong-2.jpg' />";
echo "<p class='pleft'>Contact: 038 500 500 - 081 761 4874</p>";
echo "</div>";
echo "<div id='Crate'>";



    echo "<div class='Cheader'>";
        echo "<div>";

            echo "<div>Day</div>";
            echo "<div>PSC GF</div>";
            echo "<div>Visitor GF</div>";
            echo "<div>Cart</div>";
            echo "<div>Caddy</div>";


        echo "</div>";
    while($row = mysqli_fetch_array($result)){
        echo "<div>";

            echo "<div>" . $row['day'] . "</div>";
            echo "<div>" . $row['cf'] . "</div>";
            echo "<div>" . $row['viscf'] . "</div>";
            echo "<div>" . $row['cart'] . "</div>";
            echo "<div>" . $row['caddy'] . "</div>";
            echo "<div>" . $row['note'] . "</div>";
        echo "</div>";
    }
    echo "</div>";
    echo "</div>";
    echo "</div>";
    // Free result set
    mysqli_free_result($result);
} else{
    echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);

1 个答案:

答案 0 :(得分:3)

除非您使用支持ROW_NUMBER分析函数的MySQL的新版本,否则您的查询将无法在MySQL上运行。但是,如果您想要按ID列排序的表中的前7条记录,我们可以使用LIMIT来达到同样的效果:

SELECT *
FROM bangpakong
ORDER BY ID
LIMIT 7;