3

我正在将我们的 Codeigniter (v. 3.1.11) 站点从 php 5.6 升级到 php 7.2(目前在我的 Mac 上的 localhost 上运行)。慢慢地,我找到了所有使用 count() 的实例并更正它们。我的理解是数组是可数的,但我似乎无法计算数据库调用后 Codeigniter 的 result_array() 函数返回的数组......

我的控制器的以下部分

    $reviews = $this->reviews_model->review_details($productname);
    echo "Variable is type: ".gettype($reviews);
    if (count($reviews >=1)) {
        $myreview=$reviews[0];
    } else {
        $myreview=0;
    }
    return $myreview;

在我的模型中调用此函数(请注意,我只是为了确定而回显变量类型!)

    function review_details($pagename) {
    $r = false;
    $sql = "select Reviews.*, ReviewItemLink.Item as Product, ReviewItemLink.* from Reviews LEFT JOIN ReviewItemLink ON Reviews.ReviewItemID=ReviewItemLink.ReviewItemID where pagename=? AND ReviewActive = 1 ORDER BY Date DESC";
    $query = $this->db->query($sql, $pagename);
    if ($query->num_rows() > 0):
        $r = $query->result_array();
    endif;
    return $r;
}

即使变量是一个数组

Variable is type: array

到现在为止,我仍然收到非常熟悉的警告信息:

Severity: Warning
Message: count(): Parameter must be an array or an object that implements Countable
Filename: controllers/Software.php
Line Number: 1005

Backtrace:

File: /Users/juner/Sites/html/application/controllers/Software.php
Line: 1005
Function: _error_handler
File: /Users/juner/Sites/html/application/controllers/Software.php
Line: 75
Function: _get_my_review

File: /Users/juner/Sites/html/index.php
Line: 324
Function: require_once

是否存在不可数的数组类型?任何建议/想法都会很有帮助!

4

1 回答 1

4

这部分不正确(错字?):

if (count($reviews >=1)) {
    $myreview=$reviews[0];
} else {
    $myreview=0;
}

您正在将布尔值传递给 count 函数。count($reviews >=1)转向count(true);这就是你收到警告的原因。

if (count($reviews >=1)) {应该if (count($reviews) >=1 ) {

于 2019-10-02T18:27:04.333 回答