1

Consider:

function check($string, $id, $type){
    if($type == "edit"){
        $sql = "SELECT * FROM test where first = '" . $string . "' and id = " . $id . "";
        $query = $this->db->query($sql);
        $array = $query->result();
        if(!empty($array)){
            return true; # Valid
        }else{
             $this->check($string, NULL, "add");
        }
    }else{
        $sql = "SELECT * FROM test where name = '" . $string . "'";
        $query = $this->db->query($sql);
        $array = $query->result();
        if(empty($array)){
            return true;
        }else{
            return false;
        }
    }
}

ELSE will not return the value. I tried this with both a Boolean or string value. How can I fix it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
woninana
  • 3,409
  • 9
  • 42
  • 66
  • Share is_duplicate method. – Hakan SONMEZ Aug 17 '17 at 14:29
  • @HakanSONMEZ fixed it. Just a typo. – woninana Aug 17 '17 at 14:30
  • just add `return` at that line. `return $this->check($string, NULL, "add");` – Hakan SONMEZ Aug 17 '17 at 14:30
  • Re *"ELSE will not return the value"*: Which one? There are three of them. Please respond by [editing (changing) your question](https://stackoverflow.com/posts/45737883/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the question should appear as if it was written today). – Peter Mortensen May 26 '22 at 17:28
  • Dude calm down it is from 2017. There are 3 else statements but one of them is parent so no need return. One of them has return but the other one does not have. If you can not see it line number will not help you. (Line: 9, first else in the code) – Hakan SONMEZ May 27 '22 at 10:24

1 Answers1

4
function check($string, $id, $type){
    if($type == "edit"){
        $sql = "SELECT * FROM test where first = '" . $string . "' and id = " . $id . "";
        $query = $this->db->query($sql);
        $array = $query->result();
        if(!empty($array)){
           return true; # Valid
        }else{
           return $this->check($string, NULL, "add"); # Here
        }
     }else{
         $sql = "SELECT * FROM test where name = '" . $string . "'";
         $query = $this->db->query($sql);
         $array = $query->result();
         if(empty($array)){
            return true;
         }else{
            return false;
         }
     }
 }

There is a missing return statement. You should add return to that line.

Edit from 2022: Line is 9 and else is the first one.

Hakan SONMEZ
  • 2,176
  • 2
  • 21
  • 32
  • Where is it missing? What line? Please respond by [editing (changing) your question](https://stackoverflow.com/posts/45737999/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen May 26 '22 at 17:31
  • Dude calm down it is from 2017. There are 3 else statements but one of them is parent so no need return. One of them has return but the other one does not have. If you can not see it line number will not help you. (Line: 9, first else in the code) – Hakan SONMEZ May 27 '22 at 10:24