Welcome to Techno Smarter QA, Question, Answer forum. Ask questions, receive answers, or help someone. Join Now
Welcome to Techno Smarter Q&A, where you can ask questions and receive answers from other members of the community. Ask the Technical & Programming related questions that you want. Join our community and help others .
+3 votes
318 views
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.
in PHP by (210 points) 1 3 11
Use MAX() function to retrieve the maximum value from the MYSQL database instead of MAX() function, you can use MIN() function to retrieve the minimum value from the MYSQL database using PHP.
You should use MAX( column_name) as max in the query.

4 Answers

+2 votes

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

First of all, create a query and select 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'];
by (7.1k points) 9 27 62
How to fetch and display values from the MAX to the MIN in PHP ?
+2 votes

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";
by (8.9k points) 10 24 62
+2 votes

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

Fetch largest numeric value from the MYSQL database in  PHP -

SELECT std_d, std_name, MAX(std_marks)

by (8.9k points) 10 24 62
+1 vote

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 max() function to retrieve the maximum value and display that value on-page. 

If you want to fetch the minimum number from MySQL database table, then use 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. 

by (4k points) 4 25 52
...