带有错误的查询的PHP MySQLi问题在布尔

时间:2017-04-30 23:12:01

标签: php mysql mysqli

为什么我的getLateInvoicesTotal函数返回以下错误的任何想法:

  

致命错误:未捕获错误:在布尔值上调用成员函数fetch_assoc()

此处有问题的函数:

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_date < NOW() AND invoice_due_date > NOW() and 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();
}

唯一不同的是QUERY,因为我的Open发票功能运行正常,如下所示:

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_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();
}

1 个答案:

答案 0 :(得分:1)

您在状态行之前有一个双重问题:

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

应该是

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