gpt4 book ai didi

PHP:警告:count():参数必须是数组或实现 Countable 的对象

转载 作者:行者123 更新时间:2023-12-05 00:39:45 24 4
gpt4 key购买 nike

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

Warning: count(): Parameter must be an array or an object that implements
Countable in D:\xammp\htdocs\search\index.php on line 64

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

帮帮我...错误消息。

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>

最佳答案

只需将它包装在一个检查它是否可数的条件中,所以..

// PHP >= 7.3
if(is_countable($searchdata)) {
// Do something
}

// PHP >= 7.1
if(is_iterable($searchdata)) {
// Do something
}

注意:除非你明确地定义它,否则你永远不应该假设任何事情都是真的。 count($something) 暗示它确实是一个数组或你可以计算的东西。如果情况并非总是如此,那么您在继续之前请仔细检查。

EDIT:在这种情况下,将 is_iterable() 用于 PHP >= 7.1 或 is_countable() 用于 >= 7.3。上面的片段更新了

关于PHP:警告:count():参数必须是数组或实现 Countable 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55608157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com