简体   繁体   中英

PHP echo mysqli data table from the last

i need help.. im trying to echo all the data from my database but from the last to the first. im a college student and i need ur help.

while (($row = mysqli_fetch_assoc($result)) && ($i < 6)) { // my lecture ask me to echo 5 data only
        echo "<tr>";
        foreach ($row as $field => $value) { 
            echo "<td>" . $value . "</td>";  
        }
        echo "</tr>";
        $i = $i + 1;
    }

that is my code for now, and its only echoing from the first table to the last. my lecture told me i need to echo from the last.

edit : i think i haven't give all the information.. sorry, this is the first time im here :(.

<?php 

    $i = 1;
    include_once("function/helper.php");
    include_once("function/koneksi.php");

    $query = mysqli_query($koneksi, "SELECT * FROM transaksi ");

    $pemilik = mysqli_fetch_assoc($query);


?>

<?php

$i = 1;
$sql = "SELECT mutasi, waktu_tanggal,tujuan FROM transaksi WHERE user_id='$user_id' ORDER BY waktu_tanggal";
$result = mysqli_query($koneksi, $sql); 

if(mysqli_num_rows($result) == 0) {
    ?>
    <h1>Anda belum melakukan transaksi apapun</h1>
    <?php
}else {
    echo "<br>";
    echo "<table border='1'>";
    ?> 
    <tr>
        <th>Mutasi</th>

        <th>Waktu</th>
        <th>Keterangan</th>
    </tr>
    <?php
    while (($row = mysqli_fetch_assoc($result)) && ($i < 6)) { 
        echo "<tr>";
        foreach ($row as $field => $value) {  line like this: foreach($row as $value) {
            echo "<td>" . $value . "</td>"; 
        }
        echo "</tr>";
        $i = $i + 1;
    }
    echo "</table>";
}

?>

that is the entire page.

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

The sql [ORDER BY] command is used to sort the result set in ascending or descending order.

SELECT column1, column2 FROM table_name ORDER BY column1 DESC;

Try php function mysqli_fetch_all () --- >> you don't need a while-loop. Then create a new array and populate it with the required number of records.

Like this:

$query = "SELECT TOP 5*FROM `MyTable`";
// Or alternative query: "SELECT*FROM `MyTable LIMIT 5`

$result = mysqli_query($connection, $query);

   if($result != 0) {
      $newDataArray = mysqli_fetch_all($result, MYSQLI_BOTH);
   }

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