codeigniter表连接 [英] codeigniter table join
问题描述
hi我想在1表中同时显示user表和users_profiles表:
i要链接它们,以便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表包含用户名,密码等字段,每个用户在users_profiles表中都有自己的个人资料
users users_profiles
EDIT 我尝试选择字段,但我尝试这个
时 < 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>
它返回用户个人资料中不应该的第一个值
用户和
join
函数,所以总而言之,你加入了3个表: users
, code>和
users_profiles
- >第一个具有相同的名称 - > 错误唯一/别名表。
尝试此操作(加入[]
在
在从
c> users_profiles 加入
]):
$ this-> db-> select('users.usrID,users_profiles.usrpID')
- > from('users')
- > join('users_profiles' users.usrID = users_profiles.usrpID');
$ result = $ this-> db-> get();
EDIT:
p>
要获取 users_profiles
userpNick
列:
$ this-> db-> select('users.usrID,users_profiles.userpNick')
- > from users')
- > join('users_profiles','users.usrID = users_profiles.usrpID');
$ query = $ this-> db-> get();
查看:
code><?php foreach($ query-> result()as $ row):?>
< tr class =even gradeC>
< td><?php echo $ row-> usrID< / td>
< td><?php echo $ row-> userpNick;?>< / td>
< / tr>
< ;? endforeach; ?>
hi i want to display in 1 table both the user table and users_profiles 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, i 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
users
table was in both from
and join
functions, so in sum you were joining 3 tables: users
, users
and users_profiles
-> the 2 first have the same name -> error unique/alias table.
Try this (joining [users
in from
] on [users_profiles
in join
]):
$this->db->select('users.usrID, users_profiles.usrpID')
->from('users')
->join('users_profiles', 'users.usrID = users_profiles.usrpID');
$result = $this->db->get();
EDIT:
example:
To get users_profiles
userpNick
column:
$this->db->select('users.usrID, users_profiles.userpNick')
->from('users')
->join('users_profiles', 'users.usrID = users_profiles.usrpID');
$query = $this->db->get();
view:
<?php foreach($query->result() as $row): ?>
<tr class="even gradeC">
<td><?php echo $row->usrID</td>
<td><?php echo $row->userpNick;?></td>
</tr>
<? endforeach; ?>
这篇关于codeigniter表连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!