我的Codeigniter联接查询不起作用。在一个函数中只是尝试匹配用户表中的RC代码并获取电子邮件,此功能正常工作,下一个目标是匹配文章表中的电子邮件ID并获得文章其工作正常,但是生病尝试加入用户表和文章表因为我需要得到用户的名字和来自用户表的姓氏,我不知道是否正确,请查看下面的代码。控制器部分:
public function user_article()
{
$rc=$_GET['rc'];
$data['title'] = "User Article";
if ($this->session->userdata ('is_logged_in')){
$data['profile']=$this->model_users->profilefetch();
$data['results']=$this->article_m->u_article($_GET);
$this->load->view('sd/header',$data);
$this->load->view('sd/user_article', $data);
$this->load->view('sd/footer', $data);
}
else {
}
}
我的模特:
function u_article($rc)
{
$query=$this->db->select('email')->where('rc',$rc['rc'])->get('users');
$result=$query->result_array();
if ($query->num_rows() > 0) {
$row = $query->row_array();
$array = array ('email' => $row['email'], 'a.status' => '1');
$query1=$this->db->select('a.id,title,a.status,description,image,a.email,tags,postdate,firstname,lastname,rc')->join('users u','u.email = a.email','left')->where($array)->get('articles a');
if ($query1->num_rows() > 0) {
foreach ($query1->result() as $row) {
$data[] = $row;
}
return $data;
}
else {return NULL;}
} else {return NULL;}
}
检查我的代码并告诉我我的错误提前谢谢。
视图中的错误鞋:
A Database Error Occurred
Error Number: 1052
Column 'email' in where clause is ambiguous
SELECT `a`.`id`, `title`, `a`.`status`, `description`, `image`, `a`.`email`, `tags`, `postdate`, `firstname`, `lastname`, `rc` FROM (`articles` a) LEFT JOIN `users` u ON `u`.`email` = `a`.`email` WHERE `email` = 'admin@gmail.com' AND `a`.`status` = '1'
Filename: F:\wamp\www\project\system\database\DB_driver.php
Line Number: 331
答案 0 :(得分:2)
重构您的代码。只需复制粘贴并检查其工作情况。
<强>控制器:强>
public function user_article()
{
$rc = $this->input->get('rc');
$data['title'] = "User Article";
if ($this->session->userdata ('is_logged_in'))
{
$data['profile']=$this->model_users->profilefetch();
$data['results']=$this->article_m->u_article($rc);
$this->load->view('sd/header',$data);
$this->load->view('sd/user_article', $data);
$this->load->view('sd/footer', $data);
}
else
{
}
}
<强>型号:强>
function u_article($rc)
{
$query = $this->db->select('email')->where('rc',$rc)->get('users');
$result = $query->result_array();
if ($query->num_rows() > 0)
{
$row = $query->row_array();
$array = array ('u.email' => $row['email'], 'a.status' => '1');
$query1 = $this->db->select('a.id,a.title,a.status,a.description,a.image,a.email,a.tags,a.postdate,u.firstname,u.lastname,u.rc')->join('users u','u.email = a.email','left')->where($array)->get('articles a');
if ($query1->num_rows() > 0)
{
foreach ($query1->result() as $row)
{
$data[] = $row;
}
return $data;
}
else {return NULL;}
} else {return NULL;}
}