使用PHP和MySQLi对WHERE子句之前和之后的数据进行排序

时间:2017-04-29 03:38:26

标签: php mysql mysqli

我对PHP / MySQLi有点新意,所以想到目前为止还不错,不过我有点卡住了。

我想显示OPEN发票的总和,但要将它们分开以显示到期发票和逾期/迟发票。

以下是我的功能:

function getLateInvoicesTotal() {
    // Connect to the database
    $mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME);

    // output any connection error
    if ($mysqli->connect_error) {
        die('Error : ('.$mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    $query = "SELECT SUM(`total`) as `total` FROM invoices WHERE invoice_due_date < NOW() AND status = 'open'";

    // mysqli select query
    $results = $mysqli->query($query);

    while($row = $results->fetch_assoc()) {
        $value = $row["total"];
        $value = $value / 100;
        setlocale(LC_MONETARY, 'en_GB.UTF-8');
        $value = money_format("%.2n", $value);
    print $value;
    }  

  // Frees the memory associated with a result
    $results->free();

    // close connection 
    $mysqli->close();
}

function getOpenInvoicesTotal() {
    // Connect to the database
    $mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME);

    // output any connection error
    if ($mysqli->connect_error) {
        die('Error : ('.$mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    $query = "SELECT SUM(`total`) as `total` FROM invoices WHERE invoice_due_date < NOW() AND status = 'open'";

    // mysqli select query
    $results = $mysqli->query($query);

    while($row = $results->fetch_assoc()) {
        $value = $row["total"];
        $value = $value / 100;
        setlocale(LC_MONETARY, 'en_GB.UTF-8');
        $value = money_format("%.2n", $value);
    print $value;
    }  

  // Frees the memory associated with a result
    $results->free();

    // close connection 
    $mysqli->close();
}

在我的表格中,我有两列日期,invoice_due和invoice_due_date,例如价值16/01/2017 16/06/2017

有谁知道如何修改我的2个功能来收集我想要的到期和逾期发票的数据?它工作正常,显示我只是将它们分开定义的数量。

1 个答案:

答案 0 :(得分:0)

您在查询中都使用invoice_due_date

如果您希望在一个函数中invoice_due更改invoice_due_dateinvoice_due的where查询。

希望它有所帮助。或者说清楚你想要什么?