对mysql数据库的Php查询未给出期望的结果

问题描述 投票:-1回答:1

所以我有一个数据库表,其中包含月份,以及月份和年份的数字值。 例如2020年6月6日

我有一个选择下拉列表,它会根据表中的数据动态填充。

想法是,如果当前月份为六月,则选择下拉列表仅应填充六月至十二月的月份。

我不难理解的问题是,当我运行查询时,在页面上,下拉列表仅填充了5月到9月。而如果我直接在数据库中的表上运行相同的查询,则会得到所需的结果。

以下查询:

    $yvalue = $_POST['years']; //assume this to be "2020";
    $curyear = date('Y');
    $curmonth = date('n'); // Lets also assume this to be "5";

    if ($yvalue==$curyear) {

    $query = "SELECT calmonthsID, months, value, years from calmonths WHERE years=? AND value>=? ORDER BY calmonthsID ASC";
    $stmt = $connQlife->prepare($query);
    $stmt->bind_param('ss', $yvalue, $curmonth);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($calmonthsID, $months, $value, $years);

    }else{
    $query = "SELECT calmonthsID, months, value, years from calmonths WHERE years=? ORDER BY calmonthsID ASC";
    $stmt = $connQlife->prepare($query);
    $stmt->bind_param('s', $yvalue);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($calmonthsID, $months, $value, $years);
    }

    echo "<option>- Select Month -</option>";   

    while($stmt->fetch()) {
        echo '<option value='.$value.'>'.$months.'</option>'; 
    } $stmt->close();

当上面的查询在页面上运行时,下拉列表将填充5月到9月。省略10月到12月。

[当我假设$curmonth = date('n');10时,我得到的下拉列表填充了2月到12月的几个月。省略一月。

再次当我假设$curmonth = date('n');11时,我得到的下拉列表填充了2月到12月的几个月。省略一月和十月。

[当我假设12时,缺少一月,十月和十一月。

但是直接在数据库中运行所有这些查询将获得所需的结果。因此,我对在PHP中运行查询时错在哪里或正在发生什么感到困惑。

这是数据库数据:

calmonthsID  months      value     years
1            January      1        2020
2            Febuary      2        2020
3            March        3        2020
4            April        4        2020
5            May          5        2020
6            June         6        2020
7            July         7        2020
8            August       8        2020
9            September    9        2020
10           October      10       2020
11           November     11       2020
12           December     12       2020
13           January      1        2021
....

所以我有一个数据库表,其中包含月份,以及月份和年份的数字值。例如。 2020年6月6日。我有一个选择下拉列表,该下拉列表根据表格中的数据动态填充。这个想法...

php mysql
1个回答
0
投票

似乎您已在数据库中将value列定义为varchar,而不是int。它必须为int,否则MySQL将为查询中的>=测试进行字符串比较,而不是数字比较。

© www.soinside.com 2019 - 2024. All rights reserved.