Randika Perera Randika Perera - 7 months ago 25
PHP Question

print database data with model function codeigniter

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());

Answer

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