Fetching information from the database

2019-08-06 08:31发布

问题:

MY UPDATED QUESTION IS HERE:

Fetching information from the database MYSQL & Codeigniter

please see :)


I just want to ask what's the problem with this code. I've been trying to figure this out since last night. And I again, i'm posting this question. There's something wrong with my syntax I guess.

function member_here()
{
    $this->db->select('');
    $this->db->from('membership');
    $this->db->where('username',$this->input->post('username'));
    $q=$this->db->get('');

    if($q->num_rows() > 0) {
        $data = array();
        foreach($q->result() as $row) {
            $data=$row;
        }
        return $data;
    }
}

So anyway. The output for that would be selected information from the database.

but

I figured that some parts of the code is correct because it is giving an output. But its still not right.

lets say i have this data:

|-------Name-----|-----Username-----|---------Email Address----------|
|     Marishka   |   marishkapv     |    marishka@email.com          |
|     John       |   johndoe        |   john@doe.com                 |
|     Dennis     |   dennisv        |     dennis@v.com               |

So, I logged in using Marishka. After the successful log in. The information I see in my home page is the information of dennis

Same with if I log in john

The data being fetched from the database is the last data stored. How can I select from the database which belongs to the specific username i logged in

Please answer. Thank you so much!

HERE'S AN UPDATE

this is the code in my view form

<?php
$CI =& get_instance();
$CI->load->model('membership_model');
$result = $CI->membership_model->member_here();
?>


User Information:

</br></br></br>
Name: &nbsp;<?php echo $result->first_name; echo " ";   echo $result->last_name;?><br/></br>
Email Address: &nbsp;<?php echo $result->email_address; ?><br/></br>
Date of Birth: &nbsp;<br/></br>
Gender: &nbsp;<?php echo $result->gender;   ?><br/></br>
</br></br></br>
Account Information:<br/></br></br>

Username:<?php echo $this->session->userdata('username'); ?><br/></br>
Security Question: <?php echo $result->security_question;   ?>&nbsp;<br/></br>
Security Answer: <?php echo $result->security_answer;   ?>&nbsp;<br/></br>

回答1:

In codeigniter its usually this, unless you are using a custom authentication library that does it differently.

$user_id = $this->session->userdata('account_id');

So you should use

$this->db->where('user.id',$this->session->userdata('account_id'));

I changed the lookup field from name to id, because you should perform lookups on ID's, because two people may have the same name etc. Lookup of INT's is faster as well.



回答2:

Something is wrong with your db query - your description suggests that every row is being returned (or at least Dennis' row is always being returned).

Your code just loops through all returned rows and prints the last one.

Add some debug code to dump the returned data set before looping through it and refine your query.



回答3:

You have

$data = array();
foreach($q->result() as $row) {
    $data=$row; // every time $data variable is being updated with new value
}

It'll return the last record because every time loop updates the $data with new value, if you change it to following

$data = array();
foreach($q->result() as $row) {
    $data[]=$row;
}

Then it'll keep all the rows as an array in the $data.

Alternatively you can use following code without the foreach loop

$data['result']=$q->result();

and in your view you can loop like

foreach($result as $row) {
    echo $row->name;
     ...
}

Update: You should use model, view, controller properly. You are using model in your view but you shouldn't do this. Load and use the model in your controller and from the controller load the view and pass data like,

$this->load->view('your_view_name', $data);

And loop inside the view as I mentioned above. Here is another similar answer.