数组的最小值和最大值总是相同的值

时间:2016-06-14 09:41:20

标签: php mysql arrays max min

我试图从Mysql数组中获取Min和Max值。但我总是得到与输出相同的值。

    $query1 = "SELECT * FROM  `seriennummern` WHERE id = '$i'";
    $result1 = $mysqli->query($query1);
    $countSN = $result1->num_rows;
    while($row = $result1->fetch_array())
    {
        $max = (max(array($row['Seriennummer'])));
        $min = (min(array($row['Seriennummer'])));
        print_r(array($row['Seriennummer']));

    }

print_r的输出是

Array ( [0] => 53928 ) Array ( [0] => 56945 ) Array ( [0] => 58055 ) Array ( [0] => 59149 ) Array ( [0] => 70518 ) Array ( [0] => 72020 ) Array ( [0] => 71198 ) Array ( [0] => 51161 ) Array ( [0] => 56945 ) Array ( [0] => 70665 )

我得到了数组70665中的最后一个值。

1 个答案:

答案 0 :(得分:2)

只需使用MIN and MAX查询

即可
SELECT MIN(Seriennummer) AS Min_Seriennummer, MAX(Seriennummer) AS Max_Seriennummer 
FROM  `seriennummern` WHERE GeraeteID = '$i'

你得到最小值和最大值

$result1 = $mysqli->query($query1);
$countSN = $result1->num_rows;
while ($row = $result1->fetch_array()) {
    echo $min = $row['Min_Seriennummer'];
    echo $max = $row['Max_Seriennummer'];
}

你的代码是开放的,用于sql注入检查How can I prevent SQL injection in PHP?以防止它