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());
3
  • Note: class and filenames must have first letter only upper case. Commented May 7, 2016 at 11:49
  • use this and try to display the error first stackoverflow.com/questions/17015733/… Commented May 7, 2016 at 11:51
  • yes it works..I think my main issue is i have use same name for model and controller. Commented May 7, 2016 at 13:36

2 Answers 2

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 ?>
Sign up to request clarification or add additional context in comments.

1 Comment

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..?
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.

Comments

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.