PHP:警告:count():参数必须是实现Countable

时间:2019-04-10 08:36:11

标签: php

我想选择最佳查找结果。我收到一个错误: 在这里,我在下面的文件中看到HTML表单操作和数据库连接,请检查一下。错误消息也提到了这一部分。我正在使用php 7.2而不是一些问题

  

警告:count():参数必须是实现的数组或对象
  在第64行的D:\ xammp \ htdocs \ search \ index.php中是可计数的

警告:在第37行的D:\ xammp \ htdocs \ search \ index.php中使用未定义的常量city_id-假定为“ city_id”(这将在以后的PHP版本中引发错误) >

帮我...错误消息。

PHP:

<?php
$i = 1;
if (count($searchdata) > 0) {
    foreach ($searchdata as $places) {
        echo '<tr>';
        echo '<th>' . $i . '</th>';
        echo '<td>' . $places['city'] . '</td>';
        echo '<td>' . $places['visiting_place'] . '</td>';
        echo '<td>' . $places['history'] . '</td>';
        echo '</tr>';
        $i++;
    }
} else {
    echo '<td colspan="4">No Search Result Found.</td>';
}
?>

我正在使用PHP,例如:

<?php
$searchdata = [];
$keyword = '';
if (isset($_POST['search'])) {
    $city = $_POST['city'];
    $keyword = $_POST['keyword'];
    $searchdata = $model->getVisitinPlaceData($city, $keyword);
}
?>

数据库连接和表格数据获取

<?php 
class Db {
    private $hostname = 'localhost';
    private $username = 'root';
    private $password = '';
    private $database = 'test';
    private $conn;
    public function __construct() { 
        $this->conn = mysqli_connect($this->hostname, $this->username, $this->password, $this->database); 
        if(!$this->conn) {
            echo 'Database not connected';
        }
    }
    public function getTouristCity(){
        $query = "SELECT * FROM tourist_city WHERE is_enabled = '1'";
        $result = mysqli_query($this->conn, $query);
        return $result;
    }
    public function getVisitingPlaces(){
        $query = "SELECT * FROM visiting_places WHERE is_enabled = '1'";
        $result = mysqli_query($this->conn, $query);
        return $result;
    }
    public function getVisitinPlaceData($cityid, $keyword){
        $sWhere = '';
        $where = array();
        if($cityid > 0) {
            $where[] = 'V.city_id = '.$cityid.' AND V.is_enabled = "1"';
        }

        if($keyword != '') {
            $keyword = trim($keyword);
            $where[] = "( V.visiting_place LIKE '%$keyword%' OR  V.history LIKE '%$keyword%'  OR  C.city LIKE '%$keyword%' )";
        }
        $sWhere     = implode(' AND ', $where);
        if($sWhere) {
            $sWhere = 'WHERE '.$sWhere;
        } 
        if(($cityid > 0) || ($keyword != '')) {
            $query = "SELECT * FROM visiting_places AS V JOIN tourist_city AS C ON C.city_id = V.city_id $sWhere ";
            $result = mysqli_query($this->conn, $query);
            return $result;
        }
    }
}
?>

html表单操作

<form action="" method="post" > 

            <div class="col-sm-3"> 

                <select name="city" class="form-control">

                <option value="0">Select City</option>
                <?php foreach($turistCity as $city) {
                    $checked = ($_POST['city'] == $city[city_id])? 'selected' : '';
                    echo '<option value="'.$city[city_id].'" '.$checked.'>'.$city[city].'</option>';
                }
                ?>
                </select>
            </div>
            <div class="col-sm-3"> 
             <input type="text" name="keyword" placeholder="Keword" value="<?php echo $keyword;?>" class="form-control"> 
            </div>

            <button name="search" type="submit" class="btn btn-primary">Search</button>
        </form>

3 个答案:

答案 0 :(得分:0)

只需将其包装在检查它是否可计数的条件中即可。

$("body").on('click', '#kodeSparepart', function (event) {
  var price = $(this).children('option:selected').data('price');
        $('#hargaJualTransaksi').val(price);
}

注意:除非明确定义,否则您永远都不要假设任何事情都是正确的。 // PHP >= 7.3 if(is_countable($searchdata)) { // Do something } // PHP >= 7.1 if(is_iterable($searchdata)) { // Do something } 表示期望它确实是一个数组或可以计数的东西。如果情况并非总是如此,那么在继续之前,您需要进行很多检查。

编辑:在这种情况下,对于PHP> = 7.1使用count($something)或对于> = 7.3使用is_iterable()。上面的代码段已更新

答案 1 :(得分:0)

有时您无法在使用count的地方包装所有子句,或者使用多个count的情况,只需要创建不可数的空数组,只需在实际代码之前添加即可。

if(!is_countable($your_array))$your_array = Array();

答案 2 :(得分:-1)

PHP 7.3给出了以下错误: //如果请求的页面不存在。 if($ elements ['page']> count($ elements ['pages'])){

将其更改为: if(is_countable($ elements ['page'])){

解决了!