I want to write HTML code that displays the last five rows from the database,
I have code which shows the last row, but I want to have the last five rows.
My code:
<?php
$conn = mysqli_connect("localhost", "root", "", "refd-2");
$sql = mysqli_query($conn, "SELECT * FROM last ORDER BY no DESC LIMIT 1");
$print_data = mysqli_fetch_row($sql);
echo $print_data[1];
echo "
";
Change LIMIT 1 with LIMIT 5 and fetch your resultset.
<?php
//database connectivity
$con=mysqli_connect("localhost","root","","refd-2") or die(mysqli_error());
//select values from empInfo table
$sql = "SELECT * FROM last ORDER BY no DESC LIMIT 5'";
$result = mysqli_query($con,$sql);
print_r(mysqli_fetch_array($result));
mysqli_close($con);
?>
try this :
<?php
$conn = mysqli_connect("localhost", "root", "", "refd-2");
$sql = mysqli_query($conn, "SELECT * FROM last ORDER BY no DESC LIMIT 5");
while($print_data = mysqli_fetch_array($sql)){
echo $print_data["row"];
// here you will change the name "row" to your database row name
echo "
";
}
?>
You can get the last 5 results from DB if you specify LIMIT 5
.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$con = new mysqli("localhost","root","","refd-2");
$con->set_charset('utf8mb4');
$result = $con->query("SELECT * FROM last ORDER BY no DESC LIMIT 5");
// Get all rows at once as an array of rows
$rows = $result->fetchAll();
print_r($rows);
// or use foreach loop
foreach($rows as $row) {
print_r($row);
}
The fetchAll()
method can be skipped if not needed and you can loop directly on the results.
foreach($result as $row) {
print_r($row);
}