I have a database called prices and there are 12 columns lets say.
我有一个名为价格的数据库,有12列可以说。
comp1, comp2, comp3, comp4, comp5, comp6 and so on.
comp1,comp2,comp3,comp4,comp5,comp6等。
I then want to run a query to find the lowest price stored in that row that matches an id, however I think it is treating null as lowest value as I am not getting the correct answer.
然后我想运行一个查询来查找存储在该行中与id匹配的最低价格,但我认为它将null视为最低值,因为我没有得到正确的答案。
Anyway around this? or am i doing this wrong?
不管怎么说?或者我做错了吗?
$query = "SELECT * FROM Prices WHERE id =$id" or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$comp1= $row['comp1'];
$comp2= $row['comp2'];
$comp3= $row['comp3'];
$comp4= $row['comp4'];
$comp5= $row['comp5'];
$comp6= $row['comp6'];
$comp7= $row['comp7'];
$comp8= $row['comp8'];
$comp9= $row['comp9'];
$comp10= $row['comp10'];
$comp11= $row['comp11'];
$comp12= $row['comp12'];
$min = array( $comp1, $comp2, $comp3, $comp4, $comp5, $comp6, $comp7, $comp8, $comp8, $comp10, $comp11, $comp12);
echo min($min);
}
4 个解决方案
#1
8
If you call array_filter()
on the array before calling min()
, you will purge anything that evaluates to false, including null
and 0
.
如果在调用min()之前调用数组上的array_filter(),则将清除任何计算结果为false的内容,包括null和0。
For example: $min = min(array_filter($min));
例如:$ min = min(array_filter($ min));
#2
3
Try:
$min = array( $comp1, $comp2, $comp3, $comp4, $comp5, $comp6, $comp7, $comp8, $comp8, $comp10, $comp11, $comp12);
$min = array_filter($min);
echo min($min);
#3
1
From a comment in http://php.net/manual/en/function.min.php:
来自http://php.net/manual/en/function.min.php中的评论:
I've modified the bugfree min-version to ignore NULL values (else it returns 0).
我已经修改了bugfree min-version来忽略NULL值(否则它返回0)。
<?php
function min_mod () {
$args = func_get_args();
if (!count($args[0])) return false;
else {
$min = false;
foreach ($args[0] AS $value) {
if (is_numeric($value)) {
$curval = floatval($value);
if ($curval < $min || $min === false) $min = $curval;
}
}
}
return $min;
}
?>
#4
0
Try this:
$query = "SELECT * FROM Prices WHERE id =$id" or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$i= 0;
foreach ($row as $val) {
if ($i == 0) {
$tmpMin = $val;
} else {
if ($tmpMin < $val) {
$val = $tmpMin;
}
}
}
unset($tmpMin);
echo $tmpMin;
}
#1
8
If you call array_filter()
on the array before calling min()
, you will purge anything that evaluates to false, including null
and 0
.
如果在调用min()之前调用数组上的array_filter(),则将清除任何计算结果为false的内容,包括null和0。
For example: $min = min(array_filter($min));
例如:$ min = min(array_filter($ min));
#2
3
Try:
$min = array( $comp1, $comp2, $comp3, $comp4, $comp5, $comp6, $comp7, $comp8, $comp8, $comp10, $comp11, $comp12);
$min = array_filter($min);
echo min($min);
#3
1
From a comment in http://php.net/manual/en/function.min.php:
来自http://php.net/manual/en/function.min.php中的评论:
I've modified the bugfree min-version to ignore NULL values (else it returns 0).
我已经修改了bugfree min-version来忽略NULL值(否则它返回0)。
<?php
function min_mod () {
$args = func_get_args();
if (!count($args[0])) return false;
else {
$min = false;
foreach ($args[0] AS $value) {
if (is_numeric($value)) {
$curval = floatval($value);
if ($curval < $min || $min === false) $min = $curval;
}
}
}
return $min;
}
?>
#4
0
Try this:
$query = "SELECT * FROM Prices WHERE id =$id" or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$i= 0;
foreach ($row as $val) {
if ($i == 0) {
$tmpMin = $val;
} else {
if ($tmpMin < $val) {
$val = $tmpMin;
}
}
}
unset($tmpMin);
echo $tmpMin;
}