Jump to content


Photo

mysql_fetch_array


  • Please log in to reply
4 replies to this topic

#1 Ph0enix

Ph0enix
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 05 April 2006 - 06:43 AM

Ok, im a new to php so i dont know much.
Im using this code below to get information from my table
<?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>';
}

?>

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
[url=http://www.slimphoenix.com][/url]

#2 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 05 April 2006 - 06:57 AM

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
www.karthi.us

#3 Ph0enix

Ph0enix
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 05 April 2006 - 01:42 PM

hmm i dont really understand.
Where can i find the ID number?









[url=http://www.slimphoenix.com][/url]

#4 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 05 April 2006 - 01:56 PM

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:
id     |     name     |      date       |

1      | footballkid4 |     today    |
2      | ph0enix      | yesterday |

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] * FROM table [!--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] * FROM table ORDER BY id DESC [!--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.

#5 Ph0enix

Ph0enix
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 05 April 2006 - 03:22 PM

YaY it works now! Thank you two.. you helped a lot :D
[url=http://www.slimphoenix.com][/url]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users