Jump to content

Archived

This topic is now archived and is closed to further replies.

Ph0enix

mysql_fetch_array

Recommended Posts

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 like
1.bla
2.bla
3.bla

How do i change it so it displays it
3.bla
2.bla
1.bla

So it will show the last item that was put into the table first?
Please help! Thanks

Share this post


Link to post
Share on other sites
Hello,
it is so simple, Wlcome to PHP. it is "SELECT * FROM `table_name` ORDER BY `key` DESC"

you know table_name == your table name
key == 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

Share this post


Link to post
Share on other sites
Guest footballkid4
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--]footballkid4
ph0enix[!--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--]ph0enix
footballkid4[!--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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.