2014-05-18 15:18
浏览 48


This is my model, which works when I query one table only:

function cari_username()
        $username = $this->input->post('username');
        $data = $this->db->get('user');
        return $data;

What I need is to join data from several tables, but now my code fails:

function cari_username()
        $username = $this->input->post('username');
        $data = $this->db->query("SELECT * FROM (SELECT user.id_user,mahasiswa.nama,user.password,user.username 
           FROM (`mahasiswa`) 
           LEFT JOIN `pendaftaran_anggota` ON `pendaftaran_anggota`.`nim` = `mahasiswa`.`nim`
           LEFT JOIN `anggota` ON `pendaftaran_anggota`.`id_anggota` = `anggota`.`id_anggota` 
           LEFT JOIN `user` ON `user`.`id_user` = `anggota`.`id_user` UNION 
           SELECT pelatih.id_user, pelatih.nama,user.password,user.username FROM (`pelatih`)
           LEFT JOIN `user` ON `user`.`id_user` = `pelatih`.`id_user` UNION 
           SELECT admin.id_user, admin.nama, user.password,user.username FROM (`admin`)
           LEFT JOIN `user` ON `user`.`id_user` = `admin`.`id_user`) userdata WHERE `username` LIKE username
            ORDER BY `userdata`.`id_user`

        return $data;

Please tell me how to change the query to solve this.

Thank you.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • drrvnbwle80177811 2014-05-18 20:15

    You have 2 errors in your script:


    WHERE username LIKE username will not work, as username like any php variable should be $username


    mysql doesn't know which kind of variable $username is, but we know it's a string. Therefore you need to place it within single quotes:

    WHERE `username` LIKE '$username'
    解决 无用
    打赏 举报
  • 查看更多回答(2条)

相关推荐 更多相似问题