1

我正在尝试循环显示数据库中表中的信息,但对于某些信息,我正在引用其他表。当我尝试从其他表中获取数据时,后面的任何数据都会消失。这是我正在使用的代码:

`

    //Below is the SQL query
    $listing = mysql_query("SELECT * FROM Musicians");

    //This is displaying the results of the SQL query
    while($row = mysql_fetch_array($listing))
        {




 ?>
...html here...

 <? echo $row['name']; ?>
 <? echo $row['Town']; ?>

    <?

    $CountyRef = $row['CountyId'];

    $county = mysql_query("SELECT * FROM County WHERE CouInt='$CountyRef'");



    while($row = mysql_fetch_array($county))
        {        
           echo $row['CouName'];

        }

    ?>

       <?php echo $row['instrument']; ?>

       <?php echo $row['style']; ?>`

我的问题是第二个while循环之后的所有内容都没有显示。有人有什么建议吗?

谢谢

4

3 回答 3

1

第二个循环应该说$row2$row正在被覆盖。两个变量的名称应彼此不同。

于 2013-03-26T19:00:33.617 回答
0

然后?:

while($row2 = mysql_fetch_array($county)) {        
       echo $row2['CouName'];
}
于 2013-03-26T19:02:38.903 回答
0

您可以通过一个查询来完成:

SELECT *,
(SELECT CouName FROM County WHERE CouInt=mus.CountyId) as Country
FROM Musicians mus;

您的最终代码应如下所示:

<?php
$listing = mysql_query("SELECT *,
(SELECT CouName FROM County WHERE CouInt=mus.CountyId) as Country
FROM Musicians mus;");

//This is displaying the results of the SQL query
while($row = mysql_fetch_assoc($listing))
{


echo $row['name']; 
echo $row['Town']; 
echo $row['Country']; //Thats all folks xD 
echo $row['instrument'];
echo $row['style']; 


 }   ?>

萨卢多斯 ;)

于 2013-03-26T19:01:37.410 回答