简体   繁体   中英

PHP & MySQL: Get all values from a table?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM