Whats the best way to collect all the users where server_id = X


id     server_id     user
1      2761          adam
2      2567          tom
3      2761          luke
4      2761          mike
5      2346          lucy

So what should I do to echo all users where server_id = 2761?

What output should look like...

Adam, Luke, Mike

What I think is:

$voter_count = "SELECT user FROM `voters` WHERE `server_id`='2761'";
$voter_result = mysql_query($voter_count);

if ($voter_result > 0) {
                echo implode(", ", mysql_fetch_array($voter_result));
                echo "No one has voted today using their username - be the first!";

but this results this result:

adam, adam

Thanks in adavce!

Recommended Answers

All 5 Replies

Member Avatar for diafol

OK, first up - if you can move away from the old mysql_* functions to the mysqli_* ones or even to PDO. That would be good as mysql_* support is going legs up before long.


You could do a while loop:

$voter_count = "SELECT user FROM `voters` WHERE `server_id`='2761'";
$voter_result = mysql_query($voter_count);
if (mysql_num_rows($voter_result)) {
    while($data = mysql_fetch_assoc($voter_result)){
        $users[] = $data['user'];
    echo implode(", ", $users);
    echo "No one has voted today using their username - be the first!";

This assumes that users are unique - no duplicate users in the table with the same server_id. If there are, you can use 'SELECT DISTINCT user FROM...'

commented: perfect :) +2

Thanks mate :)

Member Avatar for diafol

OK, are we solved?

We sure are :)

Do I need to an a lib to my linux server for mysqli or just change all mysql_* to mysqli_*?

Member Avatar for diafol

mysqli should be included as should PDO. Don't just randomly change mysql_ to mysqli_ since one of the downsides (IMO) to mysqli is that you need to provide the resource link (from the connection) in the function. See the manual for further info:


It will show both procedural and OOP interfaces. If you want to keep things simple and just replace as little as possible, go procedural.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.