使用PHP在Codeigniter中连接两个表

I am trying to join two tables and return an array in my Model method in CodeIgniter with php. I've gone through a few previously posted similar questions on stackoverflow and modified my code accordingly. But they don't seem to work. Hence would love to know what's wrong with the following.

I'm using the following method but am currently getting exceptions. Would appreciate suggestions in this regard.

Model Method

public function getUserDetails($username)
{
    $uid = $this->getUserUid($username);
    $this->db->select('s.uid, s.name,s.phone, s.studentId, s.type, sp.profiledesc, sp.age');
    $this->db->from('sysuser as s');
    $this->db->join('studentprofile as sp', 's.uid = sp.uid', 'left');
    $this->db->where(array('s.uid' => $uid));
    $query = $this->db->get();
    return $query->result();
}

Controller

$data1['details'] = $this->userModel->getUserDetails($username);
$this->load->view('studentDashboard/viewProfile',$data1);

View

...
<h2>
   <?php foreach($details as $detail){?>
        <?php echo $detail->s.name;?>
   <?php }?>
</h2>
...

In the view, I've also tried just echoing $detail->name but this doesn't work either.

  1. At first, use print_r($details) for checking your data. If it's returning anything or not.

  2. Then echo your value like this $detail['name']

Fixed Code:

public function getUserDetails($username)
{
    $uid = $this->getUserUid($username);
    $this->db->select("*");
    $this->db->from('sysuser');

    $this->db->join('studentprofile', 'studentprofile.uid = sysuser.uid');

    $this->db->where('sysuser.uid',$uid);
    $query = $this->db->get();
    return $query->result();
}

Look, i´m not sure that i understood your code, what means this line

$uid = $this->getUserUid($username);

You´re calling a method and sending the name to retrieve the userid, right? I´ll write that method like you should have it:

public function getUserid($user){
    $this->where->id($user);
    return $this->get('whatever table')->row();
    //i think you forgot this ->row()
}

then

public function getUserDetails($username)
{
$uid = $this->getUserUid($username); 
//here already you should bring with ->row() already

//you can use this var_dump here to confirm too
//var_dump($uid); 
//exit;

$this->db->select('s.uid, s.name,s.phone, s.studentId, s.type, sp.profiledesc, sp.age'); 
//line´s right

//the from method is disposable, so i put it into the get but here it´s right too

$this->db->join('studentprofile as sp', 's.uid = sp.uid', 'left'); //ok

$this->db->where($uid); //this line is the wronger, i´ve made the fix
//as this is an where, you´d bring only one value with ->row() from that other method

$query = $this->db->get('sysuser as s'); 
//the 'from' i putted here, just to write a line less
return $query->result();

when you need to test what you´re returng do a var_dump here
//commenting the return above
//$test = $query->result();
//var_dump($test);

}