简体   繁体   English

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

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

Here i have two tables, i want write a select results from that two tables based on mobile number. 在这里,我有两个表,我想根据移动电话号码从这两个表中写入选择结果。

First Table 第一张桌子

new_staff 新员工

staff_id              firstName        Mobile          userType

  1                  Soupranjali       9986125566       Teacher
  2                  Sujata            8553880306       Teacher

Second Table 第二表

new_student 新生

student_id           first_Name        fatherMobile        user_type

  1                  janarthan         8553880306         Student
  2                  Santanu           8277904354         Student

here 8553880306 both table available so i need output like this 在这里8553880306这两个表都可用,所以我需要这样的输出

Expected Results 预期成绩

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

So tried like this but i unable to get the answer, so please anyone help me, 如此尝试过,但我无法获得答案,所以请任何人帮助我,

my model 我的模特

 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);
            }

Updated Answer 更新的答案

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

Update answer 2 更新答案2

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

You need to check datatype of mobile column on both table. 您需要检查两个表上移动列的数据类型。

They must have same datatype to perform join operation. 它们必须具有相同的数据类型才能执行联接操作。

You can try this 你可以试试这个

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);
}

When you use result_array it will come as ZERO indexed array. 当您使用result_array ,它将作为零索引数组。 You can choose that or you can remove it as using comment on above code 您可以选择该名称,也可以使用上面的代码中的注释将其删除

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

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