【问题标题】:How to get the results from two table based on mobile number in codeigniter如何根据codeigniter中的手机号码从两个表中获取结果
【发布时间】:2017-05-09 11:59:59
【问题描述】:

这里我有两个表,我想根据手机号码从这两个表中写一个选择结果。

第一个表

新员工

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"
    }
  ]
]

【问题讨论】:

  • 打印出来(编辑你的问题)你有什么?有错误提示吗?

标签: php codeigniter model


【解决方案1】:

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

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

【讨论】:

  • 是的,我在 bigint 中给出了两个表数据类型
  • 您能否检查一下我在两个表中的选择查询是否正确?在我的查询中,我没有提供手机号码
  • 尝试在查询后使用 $this->db->last_query() 打印您的查询。我可以使用 phpmyadmin 查找记录。当我打印查询 SELECT * FROM new_staff as staff JOIN new_student as student ON staff.Mobile = student.fatherMobile 时,我得到了这个查询。如果您有正确的数据类型,您将获得结果。
【解决方案2】:

你可以试试这个

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 时,它将以零索引数组的形式出现。您可以选择它,也可以使用上述代码的注释将其删除

【讨论】:

  • @ Abdulla Nilam 先生,我尝试了您的代码,请检查我更新后的答案,我得到了这样的答案,在传达如何删除它时有一些不需要的价值
  • @subikshanM 使用这个$this->db->select('new_staff.staff_id, new_staff.firstName, new_staff.userType, new_student.student_id, new_student.firstName, new_student.user_type');
  • 看来你错过了这个$result = $query->result_array();和这个echo json_encode($result);
  • 我改变了,但我还是一样,一些不需要的值也来了
  • 添加你的答案
猜你喜欢
  • 2012-08-06
  • 2018-03-25
  • 1970-01-01
  • 2018-05-15
  • 2011-07-05
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多