codeigniter 表连接
我想在 1 个表中显示用户表和 users_profiles 表: 我想将它们链接起来,以便 usrpID = usrID,
在此过程之前,我尝试使用此代码仅显示用户表,效果很好。
控制器:
$data['query'] = $this->db->query('SELECT * FROM users_profiles');
$this->load->view('users/users_view',$data);
视图:
<?php foreach($query->result_array() as $row): ?>
<tr class="even gradeC">
<td><?php echo $row['usrID']</td>
<td><?php echo $row['usrName'];?></td>
</tr>
<? endforeach; ?>
但是当我尝试连接两个表时,它返回一个错误:这是我的代码
$this->db->select('users.usrID, users_profiles.usrpID');
$this->db->from('users', 'users_profiles');
$this->db->join('users', 'users.usrID = users_profiles.usrpID');
$result = $this->db->get();
用户表具有用户名、密码等字段,并且每个用户在 users_profiles 表中都有自己的个人资料
users users_profiles
编辑 我尝试选择字段,但是当我尝试这样做时,
<td><?php echo $row['usrID'];?></td>
<td><?php echo $row['usrName'];?></td>
<td><?php echo $row['usrpFirstName'].' '.$row['usrpLastName'];?></td>
<td><?php echo $row['usrpBday'];?></td>
<td><?php echo $row['usrpSex'];?></td>
<td><?php echo $row['usrpAddress'];?></td>
它返回了用户配置文件中不应该出现的第一个值
I want to display both the user table and users_profiles table in 1 table :
I want to link them both so that usrpID = usrID,
Before this process I tried displaying only users table using this code and it works great.
Controller:
$data['query'] = $this->db->query('SELECT * FROM users_profiles');
$this->load->view('users/users_view',$data);
View:
<?php foreach($query->result_array() as $row): ?>
<tr class="even gradeC">
<td><?php echo $row['usrID']</td>
<td><?php echo $row['usrName'];?></td>
</tr>
<? endforeach; ?>
but when I try to join two tables, it returns me an error: this is my code
$this->db->select('users.usrID, users_profiles.usrpID');
$this->db->from('users', 'users_profiles');
$this->db->join('users', 'users.usrID = users_profiles.usrpID');
$result = $this->db->get();
users table has fields like username,password, etc. and every user has his own profile in users_profiles table
users users_profiles
EDIT I tried selecting the fields but when I tried this
<td><?php echo $row['usrID'];?></td>
<td><?php echo $row['usrName'];?></td>
<td><?php echo $row['usrpFirstName'].' '.$row['usrpLastName'];?></td>
<td><?php echo $row['usrpBday'];?></td>
<td><?php echo $row['usrpSex'];?></td>
<td><?php echo $row['usrpAddress'];?></td>
it returns me the first value in users profiles in which it should not
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
users
表位于from
和join
函数中,因此您总共连接了 3 个表:users
、< code>users 和users_profiles
->第 2 个具有相同的名称 -> 错误唯一/别名表。from
中的[users
]):编辑:
示例:
试试这个(在
join
]中的[users_profiles
]上加入[ 获取users_profiles
userpNick
列:查看:
users
table was in bothfrom
andjoin
functions, so in sum you were joining 3 tables:users
,users
andusers_profiles
-> the 2 first have the same name -> error unique/alias table.Try this (joining [
users
infrom
] on [users_profiles
injoin
]):EDIT:
example:
To get
users_profiles
userpNick
column:view:
您可以参考员工表和地址表之间的连接结果示例。
You can refer this example of join result between employee and address table.