0

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();
}
user88
  • 5
  • 2
  • _"getting message "We seem to have hit a snag. Please try again later..."_ - Is that a custom message or CI's standard message on error? Either way, it doesn't really give us much to go on. Have you checked the error log for any real error messages?have you done any other debugging, to comment things out and enable them one by one to see which call it is that fails? Is `$this->db` defined in your model? – M. Eriksson Apr 27 '23 at 12:42
  • What is `$this->db`? [The CI4 docs](https://codeigniter.com/user_guide/database/queries.html) show you need to `db_connect()`, have you done that? For the results, `result_array()` is not shown in [the CI4 docs](https://codeigniter.com/user_guide/database/results.html). It looks like you maybe want `getResult('array')`? But it is a set of results, so you'll also need to iterate over it. The docs show examples. If this is your development machine, you probably want to change the environment to development, so you can see errors. https://stackoverflow.com/q/57852169/6089612 – Don't Panic Apr 28 '23 at 08:08

2 Answers2

0

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

public function getusers_data(){
    return $this->db->table('registration')->get()->getResultArray();
}
Antony
  • 3,875
  • 30
  • 32
0

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 App\Models\UserModel;

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 App\Models;

use App\Models\BaseModel;

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'];

}
DeanE10
  • 123
  • 7