-1

base on this LINK I came to with this code to make an error message if the ID does not exist

this is my code in CI model

public function user_list_update($id){
    $id = $this->db->where('id',$id);
    $query = $this->db->get('info');
    $row = $query->row();
    if($row > 0){             //line 26
        return $row;
    }else{
        echo "does not exist.";
    }
}

and in the controller

$id = $this->input->get('id');
$data["user_list"] = $this->um->user_list_update($id);
//some code here to display data of ID

and I get this error

A PHP Error was encountered

Severity: Notice

Message: Object of class stdClass could not be converted to int

Filename: models/users_model.php

Line Number: 26

Backtrace:

File: C:\xampp\htdocs\CI\application\models\users_model.php Line: 26 Function: _error_handler

P.S im new to C.I

Community
  • 1
  • 1
user3793272
  • 111
  • 8

3 Answers3

1

Use this:

 if($query->num_rows() > 0) 

Instead of:

if($row > 0){ 
Luthando Ntsekwa
  • 4,192
  • 6
  • 23
  • 52
1

The result of $row = $query->row(); is an object called $row

You cannot test an object against an integer it gives the error as you can see

So instead test that the $row variable is actually set

public function user_list_update($id){
    $id = $this->db->where('id',$id);
    $query = $this->db->get('info');
    $row = $query->row();
    if(isset($row)){             //line 26
        return $row;
    }else{
        echo "does not exist.";
    }
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
1

You can Used this solution for your problem:

public function user_list_update($id){
    $this->db->where('id',$id);
    $this->db->from('info');
    $query = $this->db->get();
    if($query->num_rows() > 0) {
        return $query->row;
    } else {
        echo "does not exist.";
    }    
}
NikuNj Rathod
  • 1,663
  • 1
  • 17
  • 26
  • Why should the OP "try this"? A **good answer** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer. – RiggsFolly Aug 23 '16 at 09:24