ecopetition Posted July 29, 2008 Share Posted July 29, 2008 Hey, In a MySQL database, how, if it is possible, can I have a script return the highest value in a particular column of a table? Thank you. Peter Link to comment https://forums.phpfreaks.com/topic/117247-select-highest-value-from-column-in-database-table/ Share on other sites More sharing options...
MasterACE14 Posted July 29, 2008 Share Posted July 29, 2008 SELECT MAX(column) FROM table WHERE whatever='whatever' I think its max. I'm not 100% sure. Link to comment https://forums.phpfreaks.com/topic/117247-select-highest-value-from-column-in-database-table/#findComment-603123 Share on other sites More sharing options...
ecopetition Posted July 29, 2008 Author Share Posted July 29, 2008 Thanks but then from the code: { $sql = "SELECT MAX(post_id) FROM posts" ; $result = $db->query($sql); while($row = $db->fetch_assoc($result)) { $highest_post[] = $row; } } "$highest_post" is an array rather than a number. Any ideas what's wrong? Thanks. Link to comment https://forums.phpfreaks.com/topic/117247-select-highest-value-from-column-in-database-table/#findComment-603132 Share on other sites More sharing options...
Gibbs Posted July 29, 2008 Share Posted July 29, 2008 Thanks but then from the code: { $sql = "SELECT MAX(post_id) FROM posts" ; $result = $db->query($sql); while($row = $db->fetch_assoc($result)) { $highest_post[] = $row; } } "$highest_post" is an array rather than a number. Any ideas what's wrong? Thanks. Remove the [] in $highest_post, 8th line. Link to comment https://forums.phpfreaks.com/topic/117247-select-highest-value-from-column-in-database-table/#findComment-603136 Share on other sites More sharing options...
ecopetition Posted July 29, 2008 Author Share Posted July 29, 2008 It's still returning an array. Link to comment https://forums.phpfreaks.com/topic/117247-select-highest-value-from-column-in-database-table/#findComment-603139 Share on other sites More sharing options...
obsidian Posted July 29, 2008 Share Posted July 29, 2008 Remember that mysql_fetch_* always returns an array of results. You need to target the specific result you are after: <?php $sql = mysql_query("SELECT MAX(col) AS max FROM my_table"); // Either get it directly like this: $val = mysql_result($sql, 0, 'max'); // Or, fetch and get the array val: $res = mysql_fetch_assoc($sql); $val = $res['max']; ?> Good luck. Link to comment https://forums.phpfreaks.com/topic/117247-select-highest-value-from-column-in-database-table/#findComment-603144 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.