我在返回true或false时几乎没有if-else语句,并且每个语句都不能正常工作。我试过很多方法,但不知道问题出在哪里。如果有人能解决这个问题,那将是非常有帮助的。
这是型号的代码
function exists($email){
$this->db->where('email',$email);
$query=$this->db->get('member_info');
echo "model is called"; // after execution this text is shown but not the others
if ($query->num_rows == 1) {
return true;
echo "i got one";
}else{
return false;
echo "i got nothing";
}
}
这是我的控制器
function is_exists($email){
echo "this is loaded"; //this line works properly
if($this->validation_model->exists($email)){
return false;
echo "true"; //this doesn't
}else{
return true;
echo "false"; // this doesn't
}
}
在打印回显部分之前返回函数。您应该在返回之前进行回声。
同时更改行以检查多个
if($query->num_rows()>0){
更新
试试这个方法。相应地替换表名、id值。
$query = $this->db->query("select id form your_table where email=".$email);
if ($query->num_rows() > 0 ){
echo "i got one";
return true;
}
else{
echo "i got nothing";
return false;
}
另外,看看你的控制器逻辑,当存在电子邮件时,它会返回false。最好改变控制器的真-假返回。
像一样尝试
if($this->validation_model->exists($email)){
echo "true";
return false;
}else{
echo "false";
return true;
}
把回波放在return
之前,它应该像一样
$query->num_rows()
因为您使用的是return
,return
之后的代码将不会执行
return false;
echo "true"; // this doesn't because you have return before this line
return true;
echo "false"; // this doesn't because you have return before this line
更改此行:
if ($query->num_rows() == 1) {
//num_rows() is a function
<?php
function exists($email){
$this->db->where('email',$email);
$query=$this->db->get('member_info');
echo "model is called"; // after execution this text is shown but not the others
//num_rows() is a function
if ($query->num_rows() == 1) {
//add message before return statement
echo "i got one";
return true;
}else{
echo "i got nothing";
return false;
}
}
您的模型应该是:
if ($query->num_rows == 1) {
return true;
}else{
return false;
}
无需打印额外的回波。与您的控制器相同
if($this->validation_model->exists($email)){
return false;
}else{
return true;
}
据我所知,您不能在return语句之后执行任何代码(在函数return中)。
我的解决方案是:
在你的控制器里像这个一样放
if($this->validation_model->exists($email)){
echo "EMAIL EXIST";
}else{
echo "EMAIL DOES NOT EXIST";
}
更改此项:
if ($query->num_rows == 1) {
到此:
if ($query->num_rows() == 1) {
并更改以下内容:
if ($query->num_rows() > 0 ){
echo "i got one";
return true;
}
else{
echo "i got nothing";
return false;
}