简体   繁体   中英

Select and echo rows from 1 - 7

Hi I have been trying to understand the use of selecting and echo only a limited row numbers from a table but I don't understand how to implemented it in the code so I hope somebody can help

I have this code where I first of want to echo in the loop only where the column id is 1-7 (this is also row 1-7) how can I do this I understand that this should be the way to do it:

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

But the above just give me a syntax error when I use that in the code the following is the code working without only picking the rows between 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);

Unless you are using a very new version of MySQL which supports the ROW_NUMBER analytic function, your query would not run on MySQL. But if you want the first 7 records from your table ordered by the ID column, we can use LIMIT to the same effect:

SELECT *
FROM bangpakong
ORDER BY ID
LIMIT 7;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM