How to display table data in reverse? (php)

旧时模样 提交于 2019-12-12 03:32:45

问题


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>';
}


回答1:


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)




回答2:


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>';
}



回答3:


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());


来源:https://stackoverflow.com/questions/14976848/how-to-display-table-data-in-reverse-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!