Sql得到多个同一行

时间:2015-11-09 10:13:49

标签: php mysql

$uid is 6

    public function getUserGroups($uid) {
    $sql = "SELECT groups.id, groups.name, groups.type , groups.img_path, DATE_FORMAT(user_group.joined_at,'%e/%c/%Y %H:%i') joined_at FROM groups JOIN user_group ON groups.id = user_group.group_id WHERE groups.id IN (SELECT group_id FROM user_group WHERE user_id = ?)";
    $query = $this->db->prepare($sql);
    $query->execute(array($uid));
    if($query->rowCount()) {
        $user_groups = $query->fetchAll();
        return $user_groups;
    }
    return false;
}

此函数可以多次从表中获取真实数据。 输出结果如下:

array (size=2)
  0 => 
    array (size=10)
      'id' => string '4' (length=1)
      0 => string '4' (length=1)
      'name' => string 'Test Grubu' (length=10)
      1 => string 'Test Grubu' (length=10)
      'type' => string 'Açık Grup' (length=11)
      2 => string 'Açık Grup' (length=11)
      'img_path' => string 'images/groupimages/11-09-2015-12-48-56_ic_participation_off.png' (length=63)
      3 => string 'images/groupimages/11-09-2015-12-48-56_ic_participation_off.png' (length=63)
      'joined_at' => string '9/11/2015 00:48' (length=15)
      4 => string '9/11/2015 00:48' (length=15)
  1 => 
    array (size=10)
      'id' => string '4' (length=1)
      0 => string '4' (length=1)
      'name' => string 'Test Grubu' (length=10)
      1 => string 'Test Grubu' (length=10)
      'type' => string 'Açık Grup' (length=11)
      2 => string 'Açık Grup' (length=11)
      'img_path' => string 'images/groupimages/11-09-2015-12-48-56_ic_participation_off.png' (length=63)
      3 => string 'images/groupimages/11-09-2015-12-48-56_ic_participation_off.png' (length=63)
      'joined_at' => string '9/11/2015 11:50' (length=15)
      4 => string '9/11/2015 11:50' (length=15)

user_group:

Group

小组:

enter image description here

我希望得到用户'所有小组。我保存到user_group,并且所有组都保持在组中。

1 个答案:

答案 0 :(得分:2)

如果我真的理解你的问题,我想是这样的:

<% form_tag contact_path, remote: true %>
   <%= text_field_tag "name %>
   <%= email_field_tag "email" %>
   <%= submit_tag %>
<% end %>