从数据库获取信息 [英] Fetching information from the database
问题描述
我的更新问题在这里:
请参阅:)
我只想问这个代码的问题是什么。我昨天晚上一直在想这个。我再次,我发布这个问题。我的语法有点错误。
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.
但
我认为代码的某些部分是正确的正在给出输出。但它仍然不对。
I figured that some parts of the code is correct because it is giving an output. But its still not right.
可以说我有这些数据:
|-------Name-----|-----Username-----|---------Email Address----------|
| Marishka | marishkapv | marishka@email.com |
| John | johndoe | john@doe.com |
| Dennis | dennisv | dennis@v.com |
所以,我使用Marishka登录。
成功登录后。我在主页中看到的信息是 dennis的信息
So, I logged in using Marishka. After the successful log in. The information I see in my home page is the information of dennis
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
请回答。非常感谢!
这里的更新
我的查看表单
<?php
$CI =& get_instance();
$CI->load->model('membership_model');
$result = $CI->membership_model->member_here();
?>
User Information:
</br></br></br>
Name: <?php echo $result->first_name; echo " "; echo $result->last_name;?><br/></br>
Email Address: <?php echo $result->email_address; ?><br/></br>
Date of Birth: <br/></br>
Gender: <?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; ?> <br/></br>
Security Answer: <?php echo $result->security_answer; ?> <br/></br>
推荐答案
您有
$data = array();
foreach($q->result() as $row) {
$data=$row; // every time $data variable is being updated with new value
}
返回最后一条记录,因为每次循环使用新值更新 $ data
(如果将其更改为以下)
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;
}
然后它将所有行作为数组保存在 $ data
。
Then it'll keep all the rows as an array in the $data
.
或者,您可以使用下面的代码: foreach
循环
Alternatively you can use following code without the foreach
loop
$data['result']=$q->result();
在你看来你可以像
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);
且 c>查看
,如上所述。这是另一个类似答案。
And loop
inside the view
as I mentioned above. Here is another similar answer.
这篇关于从数据库获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!