0

i'am new to codeigniter and i am using following cord to print database data, but it is not working..no php error only HTTP ERROR 500.

user1.php controller file-

class User1 extends CI_Controller {

    public function index()
    {
        $this->load->model("user1");

        $name = $this->user1->getUser("RANDIKA");

        $this->load->view('users', $name);  
    }

user.php model file

class User1 extends CI_Model {

    public function getUser($name){

            $sql = "SELECT * FROM `user`";
            $data['query_sql'] = $this->db->query($sql);

            return  $data['query_sql'];

    }
 }

users.php view file

print_r($name->result_array());
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • Note: `class` and `filenames` must have first letter only upper case. –  May 07 '16 at 11:49
  • use this and try to display the error first http://stackoverflow.com/questions/17015733/codeigniter-cibonfire-how-to-change-mode-from-development-to-production – JYoThI May 07 '16 at 11:51
  • yes it works..I think my main issue is i have use same name for model and controller. – Randika Perera May 07 '16 at 13:36

2 Answers2

1

User1 controller class is missing closing braces and you can't have same class name for controller and model.

Model has to be named User1_model and loaded with such name from controller.

When you're developing always have php error reporting enabled as you would see what is wrong immediately.

TheDrot
  • 4,246
  • 1
  • 16
  • 26
1

Model Code

class User1_model extends CI_Model 
{
    public function getUser($name)
    {
       $query = "SELECT * FROM `user`";
       $data = $this->db->->query($query);
       return  $data->result_array();//for single user for all return result_array() and comment $this->db->where line
    }
}

Controller Code

class User1 extends CI_Controller 
{
    public function index()
    {
        $this->load->model("user1_model");

        $data['name'] = $this->user1_model->getUser("RANDIKA");
        if($data['name'] == false )
            $data['name'] = array();            
        $this->load->view('users', $data);  
    }
}

And in View

 <?php print_r($name); // will print single users details in your case its RANDIKA ?>
Praveen Kumar
  • 2,408
  • 1
  • 12
  • 20
  • it doesn't matter single row or array. It seems not working praveen.. in model $sql = "SELECT * FROM `user`"; $data= $this->db->query($sql); i'm using this to get all data in user table. if it is like that , how can i return result and print in view..? – Randika Perera May 07 '16 at 12:46