nv39
nv39

Reputation: 535

Create list from mysql query

I have a feeling this is really simple. Here's the deal: I have a table with three columns. I want to take all the values in one of the columns and turn that into a list. I want to do this so I can transverse through the list. Each value in the list corresponds to a username. I want to take that username to access info about a user. Using this info, I can check which faculty the user is in and sort accordingly. This is what I've come up with:

    function get_users_by_faculty($faculty) { 
    global $connection;
    $query = "SELECT * FROM owner";
    $user_set = mysql_query($query); // ERROR could not establish link to server
    confirm_query($user_set);  
    foreach($user_set as $user) { //ERROR invalid argument
        $userFaculty = get_info_by_id($user["ownerId"], "ou"); 
        if($faculty == $userFaculty){
            return $user["name"];
        } else {
            return NULL; 
        }
    }

I've been quite stuck on this for a few hours.

Upvotes: 1

Views: 10173

Answers (1)

smaloron
smaloron

Reputation: 36

I don't know your fields names, but I think you could do that with an sql query.

something like that :

SELECT user.id,user.name, faculty.name 
FROM user inner join faculty on faculty.id = user.faculty_id
WHERE faculty.id=?

You should replace ? with your faculty id.

If you want a list of user names, you can use group concat :

SELECT GROUP_CONCAT(user.name SEPARATOR ';') 
FROM user inner join faculty on faculty.id = user.faculty_id
WHERE faculty.id=?
GROUP BY faculty.id

Upvotes: 2

Related Questions