skip to Main Content

I am working on codeigniter4, Right now i am trying to get all records from database but unable to find record ( getting message "We seem to have hit a snag. Please try again later…)
Here is my controller file code,Where i am wrong ?

public function getusers1(){
    $userModel = new UserModel();
    $data['result'] = $userModel->getusers_data(); 
    echo "<pre>";print_R($data['result']);
 }

Here is my model file code

public function getusers_data()
{
  $query = "SELECT * FROM registration";
  $query2=$this->db->query($query);
  return $data=$query2->result_array();
}

2

Answers


  1. You are using a Codeigniter 3 function result_array so try the following:

    public function getusers_data(){
        return $this->db->table('registration')->get()->getResultArray();
    }
    
    Login or Signup to reply.
  2. I am using the following to return all data rows for Users Table:

    Make sure you are using the right Model at the top of controller above the class name:

    use AppModelsUserModel;
    

    here is the function:

    public function getAll() {
        $users = (new UserModel)->findAll();
    
        // this will return the data to the screen
        echo '<pre>';print_r($users);echo '</pre>';die;
    }
    
    public function getOne($id) {
        $user = (new UserModel)->where('id', $id)->first(); 
    
        // this will return the data to the screen
        echo '<pre>';print_r($user);echo '</pre>';die;
    }
    

    This is the Model:

    namespace AppModels;
    
    use AppModelsBaseModel;
    
    class UserModel extends BaseModel
    {
        protected $table      = 'users';
        protected $primaryKey = 'id';
        protected $returnType     = 'object';
        protected $allowedFields = ['name', 'email', 'password', 'phone', 'address', 'last_login', 'role', 'reset_token', 'status', 'img_type'];
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search