1

我更新的问题在这里:

从数据库 MYSQL 和 Codeigniter 中获取信息

请参见 :)


我只是想问一下这段代码有什么问题。从昨晚开始,我一直在努力解决这个问题。我再次发布这个问题。我猜我的语法有问题。

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

所以无论如何。其输出将是从数据库中选择的信息。

我认为代码的某些部分是正确的,因为它给出了输出。但它仍然不对。

假设我有这些数据:

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

所以,我使用 Marishka 登录。登录成功后。我在主页看到的信息是dennis的信息

与我登录john相同

从数据库中获取的数据是最后存储的数据。如何从属于我登录的特定用户名的数据库中选择

请回答。太感谢了!

这是更新

这是我视图表单中的代码

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

3 回答 3

0

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.

于 2012-11-12T00:07:40.850 回答
0

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.

于 2012-11-11T23:56:55.453 回答
0

您的数据库查询有问题 - 您的描述表明正在返回每一行(或者至少总是返回丹尼斯的行)。

您的代码只是遍历所有返回的行并打印最后一行。

添加一些调试代码以在循环之前转储返回的数据集并优化您的查询。

于 2012-11-12T00:06:07.307 回答