gpt4 book ai didi

php - 寻找Mysql中位数

转载 作者:行者123 更新时间:2023-11-29 13:44:03 24 4
gpt4 key购买 nike

尝试查找 MYSQL 数据库中数组的中位数。

目前,我正在像这样获取我的数据:

$middleMonth = "SELECT Day, COUNT(Day) AS totalNumber FROM finalbaby GROUP BY Day ORDER BY COUNT(Day) DESC LIMIT 1, 300";
$middleResult = mysql_query($middleMonth);

然后我将它放入一个数组中,如下所示。

$names=array();
while($row = mysql_fetch_assoc($middleResult)) {
$names[] = $row['Day'];

然后我试图找到该数组的中位数:

            sort($names);
$count = count($names);
$middleval = floor(($count-1)/2);
if($count % 2) {
$median = $names[$middleval];
} else {
$low = $names[$middleval];
$high = $names[$middleval+1];
$median = (($low+$high)/2);
}

return $median;

}

var_dump($names);

我没有收到任何错误,但它使我的应用程序崩溃。

对我做错了什么有什么建议吗?

最佳答案

are you closing the while in your second code black before your third code block?

回答:

I close the while in my last }

失败...在第三个代码块中运行代码之前在第二个代码块中关闭...这只会查找中位数一次,而不是每次从 mysql 返回的行设置名称时都尝试查找中位数。第一次,名称计数为 1,然后减去 1 = 0 并除以 2。这会给你 0。然后你尝试获取 0 和 2 的余数。这会导致你的代码的其余部分失效。试试这个:

$middleMonth = "SELECT Day, COUNT(Day) AS totalNumber FROM finalbaby GROUP BY Day ORDER BY COUNT(Day) DESC LIMIT 1, 300";
$middleResult = mysql_query($middleMonth);
$names=array();
while($row = mysql_fetch_assoc($middleResult)) {
$names[] = $row['Day'];
}
sort($names);
$count = count($names);
$middleval = floor(($count-1)/2);
if($count % 2) {
$median = $names[$middleval];
} else {
$low = $names[$middleval];
$high = $names[$middleval+1];
$median = (($low+$high)/2);
}
echo $median;
var_dump($names);

//previous code
//if this code is inside function then return if not then print
//return $median;
//if this code is inside function then the return above will cancel this var_dump
//var_dump($names);

还请记住

Warning! This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

关于php - 寻找Mysql中位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17685083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com