在查询中查询查询

时间:2013-04-14 17:38:48

标签: php mysql

我有问题让这个工作。我也尝试了彼此内部的查询,但它不起作用。有人知道为什么吗?

$ligan  = mysql_query("SELECT leagueId FROM league WHERE leagueName='$leagueName'");
$laget  = mysql_query("SELECT teamid FROM team WHERE leagueId='$ligan'");
$result = mysql_query("SELECT * FROM player WHERE teamId='laget'"); 

这是打印的地方......

            <?php
            if(mysql_num_rows($result) == 0){
            echo $result;
            }
            else{
                while($row = mysql_fetch_array($result)){
                  echo "<tr>";
                  echo "<td>" . $row['playerName'] . "</td>";
                  echo "<td>" . $row['birthday'] . "</td>";
                  echo "<td>" . $row['weight'] . "</td>";
                  echo "<td>" . $row['height'] . "</td>";
                  echo "<td>" . $row['madeGoals'] . "</td>";

                  echo "</tr>";


                  }
            }
                mysql_close($db); ?>

我遇到这个问题:mysql_num_rows()期望参数1是资源,布尔值在

中给出

我相信ligan给出了一个值 laget给了很多。

你能帮忙吗?

4 个答案:

答案 0 :(得分:2)

我不知道为什么这些答案不建议您使用JOIN,我认为它更容易工作,创建它的原因是解决像你这样的问题。

是的,您有mysql_query的PHP错误,因为所有答案都已找到。

我的建议是,了解JOIN

我认为你的代码是这样的:

SELECT * FROM player
INNER JOIN team on team.leagueId = league.id
INNER JOIN league on league.name = '$leagueName'

尝试JOIN,我保证您的代码看起来更好,生活更轻松=)

答案 1 :(得分:1)

mysql_query()返回结果集,而不是结果本身。您必须从结果集中获取结果。

在这个例子中,我检查结果集以确保在访问其中的第一个结果之前有结果。

$resultset = mysql_query("SELECT leagueId from league WHERE leagueName='$leagueName'");

if( mysql_num_rows( $resultset ) > 0 ){
  $row = mysql_fetch_assoc( $resultset );
  // access the ID via $row['leagueId']
}

答案 2 :(得分:0)

$result = mysql_query(
  "SELECT * FROM player WHERE teamId IN (
    SELECT teamid from team WHERE leagueId IN (
      SELECT leagueId from league WHERE leagueName='$leagueName'
    )
  )"
);

答案 3 :(得分:0)

您不会将结果提取到变量,只需执行查询。

$result = mysql_query("SELECT leagueId from league WHERE leagueName='$leagueName'");    
$row = mysql_fetch_row($result);    
$result = mysql_query("SELECT teamid from team WHERE leagueId='$row[0]'");    
$row = mysql_fetch_row($result);    
$result = mysql_query("SELECT * FROM player WHERE teamId='$row[0]'");

while ($row = mysql_fetch_assoc($result)) {
    echo $row["name"];
    echo $row["surname"];        
}