Shyful66
Shyful66

Reputation: 1

Php does not show the lowest value

I want to see the highest and lowest Value using php. But it show the max value 9. It looks like the picture

My php code is here.

<?php 
$con = mysql_connect("localhost","root","")or die(mysql_error());
$db = mysql_select_db("simple",$con) or die(mysql_error());

$sql="SELECT* FROM simple_tb ";

 $query=mysql_query($sql) or die (mysql_error());
 while ($row=mysql_fetch_array($query))
        $id = array($row['id']);
        $id = array_filter($id);
        $min = min($id);
        $max = max($id);

{
 echo "Max Value: ".$max."<br> Min Value : ".$min;
 }
 ?>

Table

Upvotes: 0

Views: 35

Answers (1)

mrhn
mrhn

Reputation: 18916

You need to build your array up, then call min and max on it. Easiest way is to instantiate the array before the while statement, and use the [] syntax to add an element to the array like shown below. Also a note is that brackets are a nicer way to instantiate arrays, then the old array() version.

$ids = [];

while ($row = mysql_fetch_array($query))
        $ids[] = $row['id'];

$min = min($ids);
$max = max($ids);

Upvotes: 2

Related Questions