Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,584 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,959 people online right now. Registration is fast and FREE... Join Now!




ORDER BY problem

 
Reply to this topicStart new topic

ORDER BY problem

UmmmmIdk
5 Oct, 2008 - 12:29 AM
Post #1

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 10


My Contributions
CODE
<?php
include('configdb.php');
$query=mysql_query("SELECT * FROM sites ORDER BY num_votes DESC");
$i=5;
$k=0;
while($k<$i)
{
    $row=mysql_fetch_array($query);
    echo $row['sitename'];
    echo "<br />";
    $k++;
}
?>


The num_votes variable is an integer that stores the number of votes the site has accumulated. The output for this has a problem that I would like insight on. After 9 votes, the number 10 moves down between 1 and 2 when I would like it at the very top because it is greater than 9. Any insight would be greatly appreciated.
User is offlineProfile CardPM
+Quote Post

Hary
RE: ORDER BY Problem
5 Oct, 2008 - 02:02 AM
Post #2

D.I.C Head
**

Joined: 23 Sep, 2008
Posts: 205



Thanked: 15 times
My Contributions
This sounds like you have stored the num_votes as an varchar in the database, instead of a integer.
User is offlineProfile CardPM
+Quote Post

akozlik
RE: ORDER BY Problem
5 Oct, 2008 - 08:28 AM
Post #3

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 614



Thanked: 24 times
Dream Kudos: 750
My Contributions
If you only have one column you're trying to pull you should change this:

php

$query=mysql_query("SELECT * FROM sites ORDER BY num_votes DESC");


to

php

$query=mysql_query("SELECT sitename FROM sites ORDER BY num_votes DESC");


Using the * symbol selects everything from the database, and isn't very efficient if you only needed to select 'sitename'.
User is offlineProfile CardPM
+Quote Post

UmmmmIdk
RE: ORDER BY Problem
5 Oct, 2008 - 10:42 AM
Post #4

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 10


My Contributions
QUOTE(Hary @ 5 Oct, 2008 - 03:02 AM) *

This sounds like you have stored the num_votes as an varchar in the database, instead of a integer.


Yea, I overlooked that part in the database. Thanks, it works now.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 12:47AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month