Ph0enix Posted April 5, 2006 Share Posted April 5, 2006 Ok, im a new to php so i dont know much. Im using this code below to get information from my table[code]<?php$db = mysql_connect("localhost","username","pass");mysql_select_db("database name",$db);$sql = "SELECT * FROM table name";$result = mysql_query($sql);while($row = mysql_fetch_array($result)){ echo $row['field name'] . '<br>';}?>[/code]The problem is that this code displays the information like1.bla2.bla3.blaHow do i change it so it displays it3.bla2.bla1.blaSo it will show the last item that was put into the table first?Please help! Thanks Quote Link to comment Share on other sites More sharing options...
karthikeyan_coder Posted April 5, 2006 Share Posted April 5, 2006 Hello,it is so simple, Wlcome to PHP. it is "SELECT * FROM `table_name` ORDER BY `key` DESC"you know table_name == your table namekey == a key which handles the latest items. it can be `NO` or `ID` according by your table, which row is 1,2,3,...Thank you,Karthikeyan Quote Link to comment Share on other sites More sharing options...
Ph0enix Posted April 5, 2006 Author Share Posted April 5, 2006 hmm i dont really understand.Where can i find the ID number? Quote Link to comment Share on other sites More sharing options...
Guest footballkid4 Posted April 5, 2006 Share Posted April 5, 2006 Tables contain what are called 'primary keys,' which distinguish one row of data from another row of data. Generally, these keys are stored in a column called `id`, an auto_increment field. Auto_increment means that each time data is instert into the table, the key goes up one higher from the previously used key. This is to insure that no two ID's are the same, thus having a way to easily distintinush one row from the next.When using a query, MySQL, it automatically sorts by your primary key column, in ascending order, unless you specify otherwise. However, you can instruct MySQL to return the data in other formats by using the "ORDER BY" clause.For the next few examples, I will be using this data:[code]id | name | date |1 | footballkid4 | today |2 | ph0enix | yesterday |[/code]That data, using this SQL query:[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]table[/color] [!--sql2--][/div][!--sql3--]Will return:[!--html--][div class=\'htmltop\']HTML[/div][div class=\'htmlmain\'][!--html1--]footballkid4ph0enix[!--html2--][/div][!--html3--]That data, using THIS SQL query:[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]table[/color] [color=green]ORDER BY[/color] id [color=green]DESC[/color] [!--sql2--][/div][!--sql3--]Will return:[!--html--][div class=\'htmltop\']HTML[/div][div class=\'htmlmain\'][!--html1--]ph0enixfootballkid4[!--html2--][/div][!--html3--]Now, don't think that ORDER BY can only be used on an ID column, it can be used on any column which can be sorted in some way.Hope this helped you, and good luck coding. Quote Link to comment Share on other sites More sharing options...
Ph0enix Posted April 5, 2006 Author Share Posted April 5, 2006 YaY it works now! Thank you two.. you helped a lot :D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.