My goal is to get a list of all user information to display in a form/table for all members signed up for a group.
I have one table “group” which lists each user that is signed up for a group. (Users can be in multiple groups which results in an entry for each group for that member.) The fields I’m using for the purpose here are league_id and userName.
I have another table “user” which has all the user information stored. To make things simple here, the table has the fields userName, firstName and city.
I’m trying to pull a list of all the user userName from “groups” as an array, then take the results and pull the firstName and city from “user”.
I’m not having any luck and have been going around in circles. I’ve tried using a foreach loop for the second query but can’t quite seem to get that to work. Also tried an array_walk, which would get the results, but I couldn’t get it to display properly.
If anyone has any tips as to how to approach this, I’d be very grateful.
Instead of querying for all usernames first and than query for each of the individual users separately it’s more efficient to use a JOIN.
The query:
SELECT
user.userName
, user.firstName
, user.city
FROM `group`
INNER
JOIN user
ON `group`.userName=user.userName
WHERE
league_id = :league_id
ORDER BY
userName ASC