Pregunta

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?

¿Fue útil?

Solución

  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 );  
}    

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top