1

I'm trying to run this MySQL code in PHP.

SELECT DISTINCT teamid FROM teammembers INNER JOIN teams WHERE teams.id = teammembers.teamid

If I run this code in SQL, I get around 20 different values, and I would like to save this unique values in an array so I can use them later.

So I'm using this PHP code:

$totalteams = mysql_query("SELECT DISTINCT teamid FROM teammembers INNER JOIN teams WHERE teams.id = teammembers.teamid");

Now I want to check if the code is working or not, so I did:

echo $totalteams;

And as result I got:

Resource id #5

I also tried with:

echo mysql_result($totalteams,0);

And it does work that way, but that count asks me for the row number, therefore it only displays one value, and I need all of them.

Can anyone help me?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2077474
  • 353
  • 1
  • 6
  • 15

1 Answers1

5
  1. you should look at using mysqli, mysql is obsolete in the latest php (5.5)
  2. you should really look on google, I'm sure this was answered at least a million times,
  3. try something like,

while ($row = mysql_fetch_assoc( $totalteams )) {   
    print_r( $row );  
}    

Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
Joe T
  • 2,300
  • 1
  • 19
  • 31
  • 2
    Why did someone downvote? This is the correct answer. It's worth noting that if the OP wanted to echo individual values from the associative array, they should use **echo $row['column_name'];** – Joel Murphy Dec 27 '13 at 00:38
  • Thanks Joe T and Joel Murphy, both ways are working. Also, I do use mysqli, is there something wrong in the example code I posted where I'm not using it? Finally, I did search in Google but couldn't fine the right answer. – user2077474 Dec 27 '13 at 00:46
  • Hi user2077474, to use mysqli you need to replace any function that starts "mysql_" such as "mysql_query" with the mysqli equivalent ie "mysqli_query" http://www.php.net/manual/en/mysqli.query.php – Joe T Dec 27 '13 at 00:48