Jump to content

mysql_fetch_array


Ph0enix

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
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.