1

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 />";
}
2
  • may be your form using GET method. Check it. Commented Feb 23, 2013 at 6:13
  • no its post. my other model is working properly. i use $this->db->where('email',$this->input->post('email')); No problem here Commented Feb 23, 2013 at 6:23

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

this is just giving the first name in users
0

I would try:

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

2 Comments

do you think the problem is the following statement : - $user_email = $this->input->post('email');
@StacyJ pass $email from controller and test
0

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 />";
}

Comments

0

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);
 }
?>

1 Comment

Try $this->db->get_where()
0

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;
}

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.