0

I get error Fatal error: Cannot use object of type stdClass as array in my controller name is Useraccount

function userList() {   
   $result['data'] = $this->Useraccount_mod->getUser_list();
   $data['userlist']=$result['data'] ;
   $this->load->view('Useraccount/Userlist', $data);
}

and model name is Useraccount_mod

function getUser_list() {
    $query=$this->db->query("select * from users");
    return $query->result();
}

and view name is Userlist

<tbody>
    <?php
    if ($userlist > 0) {
        foreach ($userlist as $row) {
            ?>
            <tr>                                        
                <td width="100"><?php echo $row['username']; ?></td> 
                <td width="100"><?php echo $row['name']; ?></td> 
                <td width="100"><?php echo $row['address']; ?></td> 
                <td width="100"><?php echo $row['creatdate']; ?></td> 
                <td width="100"><?php echo $row['updateddate']; ?></td> 
            </tr>
            <?php
        }
    } else {
        ?>
        <tr>
            <td colspan="3" align="center"><strong>No Record Found</strong></td>
        </tr>
        <?php
    }
    ?>
</tbody>

A PHP Error was encountered Severity: Error Message: Cannot use object of type stdClass as array Filename: Useraccount/Userlist.php Line Number: 38 Backtrace:

3
  • And what's your question? What have you tried to debug or even solve this problem? Commented Jan 7, 2020 at 8:30
  • my question is how to remove Fatal error: Cannot use object of type stdClass as array Commented Jan 7, 2020 at 8:45
  • So, what have you tried to debug that error? Commented Jan 7, 2020 at 9:59

1 Answer 1

1

You are using result() function to fetch records, but this give records as object not as array. If you want to get array than you will have to use result_array().

function userList() {   
   $data['userlist'] = $this->Useraccount_mod->getUser_list();
   $this->load->view('Useraccount/Userlist', $data);
}

Method 1:

function getUser_list() {
  $query = $this->db->query("select * from users")->result_array(); //here we can use result() as well but it gives object not array
  return $query;
}


<tbody>
  <?php
  if ($userlist > 0) {
      foreach ($userlist as $row) {
          ?>
          <tr>                                        
              <td width="100"><?php echo $row['username']; ?></td> 
              <td width="100"><?php echo $row['name']; ?></td> 
              <td width="100"><?php echo $row['address']; ?></td> 
              <td width="100"><?php echo $row['creatdate']; ?></td> 
              <td width="100"><?php echo $row['updateddate']; ?></td> 
          </tr>
          <?php
      }
  } else {
      ?>
      <tr>
          <td colspan="3" align="center"><strong>No Record Found</strong></td>
      </tr>
      <?php
  }
  ?>
</tbody>

Method 2:

function getUser_list() {
  $query = $this->db->query("select * from users")->result(); 
  return $query;
}


<tbody>
  <?php
  if ($userlist > 0) {
      foreach ($userlist as $row) {
          ?>
          <tr>                                        
              <td width="100"><?php echo $row->username; ?></td> 
              <td width="100"><?php echo $row->name; ?></td> 
              <td width="100"><?php echo $row->address; ?></td> 
              <td width="100"><?php echo $row->creatdate; ?></td> 
              <td width="100"><?php echo $row->updateddate; ?></td> 
          </tr>
          <?php
      }
  } else {
      ?>
      <tr>
          <td colspan="3" align="center"><strong>No Record Found</strong></td>
      </tr>
      <?php
  }
  ?>
</tbody>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks bro for your valueable answer but I have done it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.