I don't get it...I have a titles table with 3 titles-- Manager
- Secretary
- Captain
. A user can have more than one title. So this is what I did...
'SELECT u.*, t.*, ut.*
FROM users u
JOIN user_title ut
ON ut.user_id = u.id
JOIN titles t
ON ut.title_id = t.id
WHERE u.id = 7';
Now, this works just fine when I run it in phpMyAdmin
, all 3 titles that are assigned to user.id 7 is displayed. However, when I use this in my IDE (via php), I only get back 'Manager', the first data row only. Been at this for a week now, I believe I'm losing my mind. Any suggestions? This is my actual code...
public function validateLogin($email, $pass, $title){
global $dbh;
$sql = 'SELECT u.*, p.*, t.*, ut.*
FROM passwords p
JOIN users u
ON p.user_id = u.id
JOIN user_title ut
ON ut.user_id = u.id
JOIN titles t
ON ut.title_id = t.id
WHERE u.email = :email AND p.password = :pass';
$stmt = $dbh->prepare($sql);
$stmt->execute(['email'=>$email, 'pass'=> md5($pass . $this->getSalt())]);
$data = $stmt->fetch();
if($stmt->rowCount()>0){
$stmt->closeCursor();
$_SESSION['user']['title'] = $title;
$_SESSION['user']['loggedin'] = true;
$_SESSION['user']['name'] = $data['first_name'];
return true;
}else{
$_SESSION['user']['loggedin'] = false;
return false;
}
}
Use left join instead:
'SELECT u.*, t.*, ut.*
FROM users u
LEFT JOIN user_title ut
ON ut.user_id = u.id
LEFT JOIN titles t
ON ut.title_id = t.id
WHERE u.id = 7';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.