davieboy Posted April 7, 2007 Share Posted April 7, 2007 im using this code SELECT * FROM photos WHERE STATUS = 'accepted' ORDER BY hits DESC LIMIT 5 but it only shows 5 from the DB and not the ones with the most hits ver confused David Link to comment https://forums.phpfreaks.com/topic/45993-is-there-a-code-problem-here/ Share on other sites More sharing options...
neel_basu Posted April 7, 2007 Share Posted April 7, 2007 but it only shows 5 from the DB and not the ones with the most hitsDidn't Understand at all. as you are using limit 5 it would only 5 results. Link to comment https://forums.phpfreaks.com/topic/45993-is-there-a-code-problem-here/#findComment-223486 Share on other sites More sharing options...
Dragen Posted April 7, 2007 Share Posted April 7, 2007 I think what he meant was that it show only the first five entries in the database and not five entries ordered by hits. Are you sure hits is the right name? perhaps it should be Hits? Link to comment https://forums.phpfreaks.com/topic/45993-is-there-a-code-problem-here/#findComment-223489 Share on other sites More sharing options...
neel_basu Posted April 7, 2007 Share Posted April 7, 2007 Try this SELECT * FROM `photos` WHERE `STATUS` = 'accepted' ORDER BY `hits` DESC LIMIT 5 Link to comment https://forums.phpfreaks.com/topic/45993-is-there-a-code-problem-here/#findComment-223490 Share on other sites More sharing options...
davieboy Posted April 7, 2007 Author Share Posted April 7, 2007 no still not working http://www.fsimages.net/tests/tophits.php i have hits in the DB with 141 and its only showing ones with 9 all code // Make a MySQL Connection include("config.php"); mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name) or die (mysql_error()); $query = "SELECT * FROM `photos` WHERE `STATUS` = 'accepted' ORDER BY `hits` DESC LIMIT 5"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "There are ". $row['hits'] ." ". $row['id'] .""; echo "<br />"; } Link to comment https://forums.phpfreaks.com/topic/45993-is-there-a-code-problem-here/#findComment-223502 Share on other sites More sharing options...
HaLo2FrEeEk Posted April 7, 2007 Share Posted April 7, 2007 Remove the limit and try printing out all results where status = accepted, that way you can see if it is indeed sorting by hits in descending order. Link to comment https://forums.phpfreaks.com/topic/45993-is-there-a-code-problem-here/#findComment-223507 Share on other sites More sharing options...
brissy_matty Posted April 7, 2007 Share Posted April 7, 2007 // Make a MySQL Connection include("config.php"); mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name) or die (mysql_error()); $query = "SELECT * FROM photos WHERE STATUS = 'accepted' ORDER BY `hits` LIMIT 5"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "There are ". $row['hits'] ." ". $row['id'] .""; echo "<br />"; } Would show the five highest hits. If you want it from lowest to highest then you include DESC (descending) (smallest to highest) which is why its showing the 9's instead of the 140's Unless of course you want it in that order in which case increase the limit? Link to comment https://forums.phpfreaks.com/topic/45993-is-there-a-code-problem-here/#findComment-223514 Share on other sites More sharing options...
davieboy Posted April 8, 2007 Author Share Posted April 8, 2007 its still not working http://fsimages.net/tests/tophits.php its grouping everything with the first number of the hits ie 5 and 55 are in the same line etc...(see above link) Link to comment https://forums.phpfreaks.com/topic/45993-is-there-a-code-problem-here/#findComment-224069 Share on other sites More sharing options...
neel_basu Posted April 8, 2007 Share Posted April 8, 2007 Try this <?php // Make a MySQL Connection include("config.php"); $conn = mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name, $conn) or die (mysql_error()); $query = "SELECT * FROM photos WHERE STATUS = 'accepted' ORDER BY `hits` LIMIT 5"; $result = mysql_query($query, $conn) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "There are ". $row['hits'] ." ". $row['id'] .""; echo "<br />"; } ?> You Can also try this http://zigmoyd.sourceforge.net/man/db.php#browse Link to comment https://forums.phpfreaks.com/topic/45993-is-there-a-code-problem-here/#findComment-224073 Share on other sites More sharing options...
AndyB Posted April 8, 2007 Share Posted April 8, 2007 its still not working http://fsimages.net/tests/tophits.php its grouping everything with the first number of the hits ie 5 and 55 are in the same line etc...(see above link) And I bet that's because you have the hits field defined in the database as a VARCHAR instead of an integer! Link to comment https://forums.phpfreaks.com/topic/45993-is-there-a-code-problem-here/#findComment-224115 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.