0

I have simple code for displaying images. I created table with 4 columns (ID, location, capture, equence) and inserted there 18 records. My question is: how to display all records from table in reverse mode? I need to make that the last entry will be displayed first, and the first entry displayed last.

What I need: 18-1 What I have now: 1-18

I was searching for simple codes to do that, but notwing worked at all. So i'd be very grateful if someone will help me to solve that problem.

Heres the basic code of my display script:


<?php

mysql_connect("localhost", "***", "***") or die(mysql_error());
mysql_select_db("martinidb1337") or die(mysql_error());

$result = mysql_query("SELECT * FROM klpgalerija") or die(mysql_error()); while($row = mysql_fetch_array( $result )) {

    echo '<p><img src="'.$row['location'].'"></p>';
}

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2090528
  • 21
  • 1
  • 4

3 Answers3

3

You have to use MySQL ORDER BY clause for that,

SELECT * FROM klpgalerija ORDER BY id DESC

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated.

So use either PDO or MySQLi (IMO PDO is way to go)

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
1

Changed query from "SELECT * FROM klpgalerija" to "SELECT * FROM klpgalerija ORDER BY ID DESC"

<?php

mysql_connect("localhost", "***", "***") or die(mysql_error());
mysql_select_db("martinidb1337") or die(mysql_error());

$result = mysql_query("SELECT * FROM klpgalerija ORDER BY ID DESC") or die(mysql_error()); while($row = mysql_fetch_array( $result )) {

    echo '<p><img src="'.$row['location'].'"></p>';
}
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
1

add an order by desc clause in your sql query

$result = mysql_query("SELECT klpgalerija.* FROM klpgalerija order by klpgalerija.ID desc") or die(mysql_error());
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
novalagung
  • 10,905
  • 4
  • 58
  • 82