Jump to content

Array containing numbers: simple way to extract the largest number?


Jodha

Recommended Posts

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 :)

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...

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?

 

 

 

 

 

 

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 ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.