Jodha Posted February 4, 2009 Share Posted February 4, 2009 Hi! Assume I've queried a database and obtain values from column 'X'. This only contains numbers. Can I obtain a variable which will equal the highest number in that column? I can only find info on how to add all the column numbers, select rows for certain number values, or list all the column numbers, sorted or unsorted Quite happy with that, just want to be able to extract the highest number from the selection ??? Help muchly appreciated Link to comment https://forums.phpfreaks.com/topic/143842-array-containing-numbers-simple-way-to-extract-the-largest-number/ Share on other sites More sharing options...
genericnumber1 Posted February 4, 2009 Share Posted February 4, 2009 Something like SELECT MAX(x) as max_x FROM table would work fine for a query... From an array? max() (the php function) would be fine Link to comment https://forums.phpfreaks.com/topic/143842-array-containing-numbers-simple-way-to-extract-the-largest-number/#findComment-754752 Share on other sites More sharing options...
Jodha Posted February 4, 2009 Author Share Posted February 4, 2009 Cool, Actually a mate's just called with similar solution: code: $query = "SELECT MAX(column) FROM table"; $depth = myqsli($cnx,$query) or die ("die message"); But echo $depth; displays "Catchable fatal error: object of my class mysqli_result could not be converted to a string on 'line reference of code'" plus I am getting the same message which ever way I go about this now. Assuming highest value is '5' I could do with $depth = 5, or what ever it gets set to in the db... Link to comment https://forums.phpfreaks.com/topic/143842-array-containing-numbers-simple-way-to-extract-the-largest-number/#findComment-754777 Share on other sites More sharing options...
genericnumber1 Posted February 4, 2009 Share Posted February 4, 2009 You might want to see this page: http://www.phpfreaks.com/forums/index.php/topic,95441.0.html Link to comment https://forums.phpfreaks.com/topic/143842-array-containing-numbers-simple-way-to-extract-the-largest-number/#findComment-754800 Share on other sites More sharing options...
Jodha Posted February 5, 2009 Author Share Posted February 5, 2009 OK, I've looked at the link provided, it looks like it refers to something else I am already doing elsewhere which I am fine on. In MyphpAdmin I type this: SELECT MAX(column) FROM table I get my magic number 'X', I know the SQL query works. In my script I am currently trying: _________________________________________________________ $cnx = connections variables, all present and correct $query = "SELECT MAX(column) FROM table"; $result = mysqli_query($cnx.$query) or die ("die statement"); $row = mysqli_fetch_[ row/assoc/array - same output for any ]($result); echo $row _________________________________________________________ I echo $row so I can see if it agrees with the number I obtain from the direct SQL query typed into MyphpADMIN. All I see displayed is "Array". - Using the actual table column name as a variable displays nothing - Using the actual table column name without a $ displays the column name, not max number. __________________________________________________________ From other experiments I am pretty sure I can obtain the highest value in a column on a table, and just display it in a couple of code lines following the query. Using 'while' statements seem to be more relevant to when I am waling through the content of a table, displaying items from each row and I go through each row. I can do this aspect no probs Thoughts anybody? Link to comment https://forums.phpfreaks.com/topic/143842-array-containing-numbers-simple-way-to-extract-the-largest-number/#findComment-755149 Share on other sites More sharing options...
Jodha Posted February 5, 2009 Author Share Posted February 5, 2009 Sorted: Below the query script I need to include the following - $row = mysqli_fetch_assoc($result); $max_'variable name' = $row[0]] echo $max_'variable name' I can now see my maximum variable number as $max_'variable name', thus can use $max_'variable name' is other parts of my code where I need to. I knew it would be quite simple in terms of some additional code with correct syntax after the initial query. I've now got this working to pull two max variable values out of my table The rest of my code should be easy now I have obtained two maximums I needed Link to comment https://forums.phpfreaks.com/topic/143842-array-containing-numbers-simple-way-to-extract-the-largest-number/#findComment-755160 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.