繁体   English   中英

如何在Codeigniter中基于手机号码从两个表中获取结果

[英]How to get the results from two table based on mobile number in codeigniter

在这里,我有两个表,我想根据移动电话号码从这两个表中写入选择结果。

第一张桌子

新员工

staff_id              firstName        Mobile          userType

  1                  Soupranjali       9986125566       Teacher
  2                  Sujata            8553880306       Teacher

第二表

新生

student_id           first_Name        fatherMobile        user_type

  1                  janarthan         8553880306         Student
  2                  Santanu           8277904354         Student

在这里8553880306这两个表都可用,所以我需要这样的输出

预期成绩

    {
  "status": "Success",
  "Profile": [
    {
      "staff_id": "2",
      "firstName": "Sujata",
      "userType" : "Teacher"
    },
    {
      "student_id": "1",
      "firstName": "janarthan",
      "user_type" : "Student"
    },
  ]
}

如此尝试过,但我无法获得答案,所以请任何人帮助我,

我的模特

 public function android_memberList($mobile)
            {
                $this->db->select('new_staff.staff_id, new_staff.firstName, new_staff.userType, new_student.student_id, new_student.first_Name, new_student.user_type');
                //$this->db->select('*');
                $this->db->from('new_staff');
                $this->db->join('new_student', 'new_student.fatherMobile  = new_staff.Mobile');
                $query = $this->db->get();

                # result_array is used to convert the data into an array
                $result = $query->result_array(); // or  $result = $query->result_array()[0]; 
                echo json_encode($query);
            }

更新的答案

     [
  {
    "staff_id": "2",
    "firstName": "Sujata",
    "userType": "Teacher",
    "student_id": "1",
    "first_Name": "janarthan",
    "user_type": "Student"
  }
]

更新答案2

    [
  [
    {
      "staff_id": "2",
      "firstName": "Sujata",
      "userType": "Teacher",
      "student_id": "1",
      "first_Name": "janarthan",
      "user_type": "Student"
    }
  ]
]

您需要检查两个表上移动列的数据类型。

它们必须具有相同的数据类型才能执行联接操作。

你可以试试这个

public function android_memberList($mobile)
{
    $this->db->select('new_staff.staff_id, new_staff.firstName, new_staff.userType, new_student.student_id, new_student.firstName, new_student.user_type');
    //$this->db->select('*');
    $this->db->from('new_staff');
    $this->db->join('new_student', 'new_student.fatherMobile  = new_staff.Mobile');
    $query = $this->db->get();

    # result_array is used to convert the data into an array
    $result = $query->result_array(); // or  $result = $query->result_array()[0]; 
    echo json_encode($result);
}

当您使用result_array ,它将作为零索引数组。 您可以选择该名称,也可以使用上面的代码中的注释将其删除

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM