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 />";
}
share|improve this question
may be your form using GET method. Check it. – Yogesh Suthar Feb 23 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 – Stacy J Feb 23 at 6:23

4 Answers

I would try:

$query_userdetails = $this->db->query("SELECT * FROM users WHERE email = '". $user_email ."'");
share|improve this answer
I tried this, no success – Stacy J Feb 23 at 6:22
do you think the problem is the following statement : - $user_email = $this->input->post('email'); – Stacy J Feb 23 at 6:47
@StacyJ pass $email from controller and test – raheel shan Feb 23 at 9:20

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

share|improve this answer
this is just giving the first name in users – Stacy J Feb 23 at 6:41

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

share|improve this answer

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 />";
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.