douyi9705
2013-12-27 00:32
浏览 36
已采纳

PHP和MySQL:从表中获取所有值?

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?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试用PHP运行这个MySQL代码。

 < 代码> SELECT DISTINCT teamid来自teammembers INNER JOIN团队WHERE teams.id = teammembers.teamid 
   
 
 

如果我在SQL中运行此代码,我会得到大约20个不同的值, 我想将这个唯一值保存在一个数组中,以便以后可以使用它们。

所以我正在使用这个PHP代码:

   $ totalteams = mysql_query(“SELECT DISTINCT teamid FROM teammembers INNER JOIN团队WHERE teams.id = teammembers.teamid”); 
   
 
 

现在我要查看 如果代码工作与否,我做了:

  echo $ totalteams; 
   
 
 

结果 我得到了:

 资源ID#5 
   
 
 

我也尝试过: \ n

  echo mysql_result($ totalteams,0); 
   
 
 

它确实有效,但是这个数字要求我输入行号, 因此它只显示一个值,我需要所有这些值。

可以 一个人帮帮我?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douchen1988 2013-12-27 00:35
    已采纳
    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 );  
    }    
    

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题