Stacy J
Stacy J

Reputation: 2781

select-from-where query not working in codeigniter model

I am trying to select the details of the users from a table in mysql database but it is not working. This is the code in my model :-

public function getuserdetails()
{
        $user_email = $this->input->post('email');
        $query_userdetails = $this->db->query("SELECT * 
                                               FROM users WHERE email = '$user_email' ");

        return $query_userdetails->result_array();
}

This is not working. But if I put the actual email id in the query instead of $user_email it works but not properly i.e if I use :-

$query_userdetails = $this->db->query("SELECT * FROM users WHERE 
                                       email = '[email protected]' ");

In this case it returns a result. My controller code to accept the result is :-

$data['details'] = $this->model_userdetails->getuserdetails();

But the problem is that when I access $details in view :-

echo $details['name']."<br />";

it does not recognize 'name'. name is the field in the database where the name of the users are stored. But if I try to retrieve it in a foreach loop it is working :-

foreach($details as $udetails)
{
     echo $udetails['name']."<br />";
}

Upvotes: 1

Views: 9377

Answers (6)

Waseem shah
Waseem shah

Reputation: 450

Try this please it will help you. Model

 public function getuserdetails()
    {
        $user_email = $this->input->post('email');
        $this->db->select("*");
        $this->db->from('users');
        $this->db->where('email',$user_email);
        $query = $this->db->get();
        $result= $query->result();
    }

Controller

$data['details'] = $this->model_userdetails->getuserdetails();

view

foreach($details as $detail)
{   
   echo $detail->name;
   echo $detail->email;
}

Upvotes: 0

munaz
munaz

Reputation: 166

You can try like This:

[CONTROLLER]

$data = array();
$data['udetails'] = $this->UserModel->getuserdetails();
$this->load->view('welcome_message',$data);

[MODEL]

public function getuserdetails() {
    $user_email = 'webmaster';
    $userdetails = $this->db->query("SELECT * FROM users WHERE umail = '$user_email' ");
    return $userdetails->result();
}   

[VIEW]

<?php 
 foreach($udetails as $row) 
 {
    echo($row->uname.'<br/>');
    echo($row->uname);
 }
?>

Upvotes: 0

Winston
Winston

Reputation: 1805

You have to write like this

public function getuserdetails()
{
    $user_email = $this->input->post('email');
    $this->db->where('email', $user_email)
             ->get('users')
             ->result_array();
}

After this, in view, you must to write like this

foreach($details as $udetails)
{
     echo $udetails['name']."<br />";
}

Upvotes: 0

Stacy J
Stacy J

Reputation: 2781

I have solved the part where only foreach loop would work.

If we use row_array instead of returning result_array() the foreach constraint to diplay goes away.

Now I want to select the name from the database where email is $user_email

Upvotes: 0

Run queries as per codeigniter suggestion

$query_userdetails = $this->db->query("SELECT * FROM users WHERE email = ?", array($user_email));

http://ellislab.com/codeigniter/user-guide/database/queries.html search for Query Bindings

Upvotes: 1

Staffan Wallin
Staffan Wallin

Reputation: 1

I would try:

$query_userdetails = $this->db->query("SELECT * FROM users WHERE email = '". $user_email ."'");

Upvotes: 0

Related Questions