比较三个表值,并使用codeigniter从这两个表中检索一个值


Compare three table values and retrieve a value from both the table using codeigniter

我有三个表

student_details
   1.ID
   2.studentname
   3.deptname
   4.deptcode
  login_details
   1.id
   2.username
   3.password
   4.deptcode
 dept_details
   1.id
   2.deptcode
   3.deptname

从这些表中,我需要检查login_details表中的用户名和密码,并通过检查student_details的值从dept_details表中检索deptname。deptcode==login_details.deptcode=student_dedetails.ddeptcode..这些概念,那么我如何在codeigniter中实现这些步骤,请帮助某人。。提前感谢

在codeigniter中,您可以使用Active Records执行此操作,也可以通过传递自定义查询(如)

$username="test";
$password="password";
$query="SELECT dd.deptname,sd.studentname FROM dept_details dd
INNER JOIN login_details ld ON  (dd.deptcode = ld.deptcode)
INNER JOIN student_details sd ON  (ld.deptcode = sd.deptcode)
WHERE ld.username='".$username."' AND ld.password='".$password."' ";
$result=$this->db->query($query)->result();

$this->db->select('dd.deptname,sd.studentname');
$this->db->from('dept_details dd');
$this->db->join('login_details ld', 'dd.deptcode = ld.deptcode');
$this->db->join('student_details sd', 'd.deptcode = sd.deptcode');
$this->db->where('ld.username', $username);
$this->db->where('ld.password', $password);
$result = $this->db->get();

并确保您已将database库加载到autoload.php中,如下所示$autoload['libraries'] = array("database");

这是活动记录的帮助希望这是有意义的

student_detailslogin_details应该是一个表。一个用户可以有多个登录详细信息吗?@tomexans是对的,因为"student_id"是你唯一的id,你应该用这个id连接你的表。

我在想这个结构:

学生

student_id
student_name
username
password
dept_id

dept_details:

dept_id
dept_code
dept_name