How to retrieve the highest numeric value from MYSQL database in PHP ?

3
(2.5K Views)

×

Please Login or Register to process.

I wish to write a code in PHP to retrieve the highest value from an array of numeric values stored in a table in mysql database. I addition, i also need to write a corresponding javascript code on the HTML FORM to do a similar thing as the PHP code.
asked 18 Mar 2020 in PHP by umgbemena(210 Points)

Share

4 Answers
answered 18 Mar 2020 by vishalrana1(7.3K Points)
2

Thanks for posting your question. If you want to retrieve the highest value of data from the MYSQL database using PHP programming then you should use the maximum (max)  function and then you can use that value where you want to use it. Retrieving the largest value from the MYSQL database using PHP. 

First of all, create a query and select the maximum value of the table. 

Use mysqli_fetch_array to fetch that value and use max with the array. You will get the highest value from the MYSQL database using PHP. 

$sql= mysqli_query( "SELECT MAX( id ) AS max FROM `db_table`;" );
$res = mysqli_fetch_array( $sql);
$highestValue = $res['max'];

 


Comment

answered 18 Mar 2020 by shakti(9.1K Points)
2

You can use the query below to retrieve the highest numeric value from the MYSQL database in PHP. 

"SELECT MAX(student_no) AS Higheststudent_no
 FROM records";

 


Comment

answered 18 Mar 2020 by shakti(9.1K Points)
2

I have written another answer. If you want to fetch the largest number from the MYSQL database with another column then you can use the query like below 

Fetch the largest numeric value from the MYSQL database in  PHP -

SELECT std_d, std_name, MAX(std_marks)

 


Comment

answered 19 Mar 2020 by priyasharma4005(4.1K Points) (edited)
1

Maybe I am late in answering and the answer will be used for future reference. I read your description and I can understand your concern. You want to fetch the maximum  (Highest number ) value from the MYSQL database table in PHP. I'm right? 

Let's discuss the max() function. The max() function is an in-built function in PHP. You can use the max() function to retrieve the maximum value and display that value on-page. 

If you want to fetch the minimum number from the MySQL database table, then use the min() function of PHP. The min() function is also a in-built function in PHP. 

<?php 

$result =mysqli_query($mysqli, "SELECT  MAX( column_name) as max , MIN(column_name) as min FROM mytable_name");

      while($res = mysqli_fetch_array($result)) { 



           $max = $res['max']; 

           echo 'Highest Number :'.$max.'<br>'; 



 $min=$res['min'].'<br>'; 

         echo 'Lowest Number :'.$max.'<br>'; 

 }

?> 

This will produce the highest and lowest number from the database table. 

 


Comment

Related Questions :-

Your Answer

You can post your answer after login..