我有一个查询,我用来获取我现在的id之后的下一个id但是当我现在的id之后没有更多的id我似乎不知道如何告诉我的代码这个查询没有返回id。
public function select_pers_id($current_id){
$que= $this->query("SELECT * FROM personality_question WHERE personality_question_id > $current_id ORDER BY $current_id ASC LIMIT 1");
$rowp = $que->fetch_assoc();
return $rowp['personality_question_id'];
}
这就是我称呼它的方式
$nid = $this->select_pers_id($currentid);
尝试在不同时间使用这些代码块告诉我的系统查询没有返回id,但是没有id已经工作
if($nid=='null'){
$sign = 'false';
echo json_encode(array("value" => $sign, "value2" => $error));
}
if($nid==''){
$sign = 'false';
echo json_encode(array("value" => $sign, "value2" => $error));
}
if(empty($nid)){
$sign = 'false';
echo json_encode(array("value" => $sign, "value2" => $error));
}
被困在这里好几个小时
答案 0 :(得分:1)
我会将您的select_pers_id()
更改为:
public function select_pers_id($current_id){
$que= $this->query("SELECT * FROM personality_question WHERE personality_question_id > $current_id ORDER BY $current_id ASC LIMIT 1");
if ($que->num_rows > 0) {
$rowp = $que->fetch_assoc();
return $rowp['personality_question_id'];
} else {
return -1;
}
}
首先检查SELECT
查询是否通过使用 计算返回的行来返回结果 [编辑:与@ CD001 enlightenment,您需要使用rowCount()
num_rows
] ,如果是,则返回所需数据,否则返回无效id
(在此示例中为-1)
答案 1 :(得分:0)
您的验证正在寻找NULL或者它是否为空。
当$ nid不存在时抛出并出错我试试:
if(!$nid) {
$sign = 'false';
echo json_encode(array("value" => $sign, "value2" => $error));
}
答案 2 :(得分:0)
您可以使用num_rows
检查回复的数量,然后返回符合它的结果
public function select_pers_id($current_id){
$que= $this->query("SELECT * FROM personality_question WHERE personality_question_id > $current_id ORDER BY $current_id ASC LIMIT 1");
if ($que->num_rows > 0) {
return null;
}
$rowp = $que->fetch_assoc();
return $rowp['personality_question_id'];
}
然后检查
$nid = $this->select_pers_id($currentid);
if($nid==null){
$sign = 'false';
echo json_encode(array("value" => $sign, "value2" => $error));
}