Search code examples
codeigniter

Echo an error message if query result is empty instead of ERRORS (Codeigniter)


My target is to show an echo "No data" when the query result is empty. What happening currently is that when my query is empty, it shows an error. (Please see the picture below for the error) I provided my codes below and my screenshot. Any help will be appreciated. Thank you

enter image description here

Controller:

public function new1($arenaID=0) {
       
        $data['activeNav'] = "";
        $data['subnav'] = "arenas";
        $this->header($data);
        $this->nav();
        
        $data['k1']=$this->arenas->meron1();
       
        $this->load->view('new', $data);
        $this->footer();
       
    }

Model:

function meron1(){
      
        $query = $this->db->query("SELECT fightID FROM fight_entries where position='meron' ORDER BY fightID DESC LIMIT 1");  
        
            return  $query->row()->fightID+1;
          
        
         
    }

Solution

  • A better way to do this is just to check the number of rows being returned.

    function meron1(){
     $query = $this->db->query("SELECT fightID FROM fight_entries where position='meron' ORDER BY fightID DESC LIMIT 1");
        if($query->num_rows() > 0){
         $query = $query->rows();
         return  $query->fightID+1;        
        }else{
        $message = "No Data";
        return $message;
        }