我一直在寻找其他问题,但这似乎与我自己的代码有关,我自己也无法识别问题。
我有以下查询成功执行并返回id列我要求:
SELECT city_id FROM location_city WHERE c_name = 'cityname'
然而,当我使用我的功能来检查城市是否存在时,无论我作为参数通过哪个现有城市,我总是得到0:
// check if city already exists
public function is_city($city) {
$mysqli = $this->aet->getAetSql();
if ($stmt = $mysqli->prepare('SELECT city_id FROM location_city WHERE c_name = ?')) {
$stmt->bind_param('s', $city);
$stmt->execute();
$stmt->store_result();
$stmt->num_rows === 1 ? $stmt->bind_result($city) : $city = FALSE;
}
return $city;
}
我一直在做一些测试:
$city = 'Elche';
echo $functions->is_city($city);
if (FALSE !== $functions->is_city($city)) echo 'NOT FALSE'; else echo 'FALSE';
// 0
// NOT FALSE
我还检查了$stmt->execute()
,它返回TRUE,没有错误(echo $stmt->error;
)
我还有另一个功能,可以生成城市列表,并且工作正常。唯一的区别是我没有使用$stmt->store_result();
或$stmt->num_rows
。我尝试在is_city()
中对这两个人进行评论,但仍无法正常工作。
有什么想法吗?谢谢!
答案 0 :(得分:1)
这可以解决您的问题:
// check if city already exists
public function is_city($city) {
$mysqli = $this->aet->getAetSql();
$stmt = $mysqli->prepare('SELECT city_id FROM location_city WHERE c_name = ?');
if ( FALSE == $stmt ) {
return FALSE;
}
$stmt->bind_param('s', $city);
$stmt->execute();
$stmt->bind_result( $city_id );
if ( $stmt->fetch() ) {
return $city_id;
}
return $city_id;
}