Home > database >  Letting 2 users see the same row
Letting 2 users see the same row

Time:12-20

I'm trying to have 2 users see the same table entry in pdo. The way i did it seems to be giving me different data from both tables.

Group A: Rentals < This is the data i'm looking for only, has a rental_id Group B: rental_group < has a rental_id and a user_id.

What i'm trying to accomplish here is matching the rental_id's for each user. so if theres multiple users under the same rental_id, so they can all see the same rentals row.

I've tried:

public function getSomething() {
        $stmt = $this->connect()->prepare("SELECT * FROM rentals JOIN rental_group ON rentals.id = rental_group.rental_id WHERE rental_group.worker = ?");
        $id = $_SESSION['userid'];
        $stmt->execute([$id]);
        if($stmt->rowCount()) {
            while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
                $returnResults[] = $row;
            }
            return $returnResults;
        }
    }

This seems to semi work i guess, but its giving me different data if i was to echo $row['id'] the id isn't coming from the rentals table.

rental_group.worker is the user.

CodePudding user response:

Looks like you need to be more specific with your query and the rental_group id might be overriding the rentals id. Try something like this:

SELECT rentals.*, rental_group.rental_id, rental_group.worker
FROM rentals
JOIN rental_group ON rentals.id = rental_group.rental_id
WHERE rental_group.worker = ?
  • Related