-3

I am trying to display the top 5 newest itmes added to the database. I have it showing all of them on my items.php page, but I would like to put 5 on the dashboard.php page.

I am just learning php/mysql and have only been doing it for about 4 days and I feel I am learning pretty quick, but I cannot find this when doing a search.

Here is my code to get the full list of items.

 <?php
        $SQL_GetProducts = "SELECT * FROM `new_equip` ORDER BY `id` DESC;";
        $R_GetProducts = mysql_query($SQL_GetProducts, $Link);
            while ($row = mysql_fetch_assoc($R_GetProducts))
            {
            $eid = $row['id'];
            $name = $row['name'];
            $model = $row['model'];
            $desc = $row['desc'];
            $imagename = $row['image']; 
            $date = $row['date'];

            if (!file_exists("../UImages/" . $imagename) || strlen($imagename) < 5)
            {
            $imagename = "NoImage.jpg";
            }
            ?>  

<tbody>
        <tr>
            <td>
              <div class="thumbnail">
                <div class="thumbnail-view">
                  <a href="../UImages/<?php echo $imagename; ?>" class="thumbnail-view-hover ui-lightbox"></a>
                    <img src="../UImages/<?php echo $imagename; ?>" width="125" alt="Gallery Image" />
                </div>
              </div> <!-- /.thumbnail -->
            </td>
            <td><?php echo $name; ?></td>

            <td>
              <p><?php echo wordwrap($desc, 90, "<br />");?> </p>
            </td>
            <td><?php echo $date; ?></td>

            <td class="text-center">
              <button class="btn btn-xs btn-primary"><i class="fa fa-pencil"></i></button>
              &nbsp;
              <button class="btn btn-xs btn-secondary"><i class="fa fa-times"></i></button>
            </td>
          </tr>

          <?php } ?>


        </tbody>

From what I have read and seen I need to have something like this

$SQL_GetProducts = "SELECT * FROM `new_equip` ORDER BY `id` DESC LIMIT 0, 5";

Then I read something about a return $query->fetch();

Any help would be greatly appreciated.

WebbieWorks
  • 163
  • 1
  • 17
  • You need to use `LIMIT 0,5` in your current query. That's it! – RNK Oct 08 '14 at 15:25
  • 2
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 08 '14 at 15:25
  • 1
    So, have you tried it? Did you get errors? Are you checking for errors? What exactly is the issue? – Funk Forty Niner Oct 08 '14 at 15:26

1 Answers1

0

Welcome
I advice you to don't use $query->fetch();
Your code is right , but I I suggest to write :

SELECT * FROM `new_equip` ORDER BY `id` DESC LIMIT 5

And don't forget : You should delete ";" after DESC here :

SELECT * FROM `new_equip` ORDER BY `id` DESC;
Ameer Ahmed
  • 144
  • 1
  • 2
  • 9
  • I appreciate it, That is what I used and it has worked. Guess I should have tried it before I asked. Thanks for everyones time and answer. – WebbieWorks Oct 08 '14 at 19:20