mysql查询中的mysql查询

时间:2022-09-18 13:04:52

I'm trying to display information from a table in my database in a loop, but for certain information, I'm referencing other tables. When I try to get data from other tables, any data following will disappear. here is the code I am using:

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

`

    //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']; ?>`

My problem is that everything after the second while loop is not displaying. Anyone have any suggestions?

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

Thanks

3 个解决方案

#1


0  

You can acomplish that with a one single query:

您可以使用一个查询来完成它:

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

You final code should looks like:

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

<?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']; 


 }   ?>

Saludos ;)

#2


1  

Second loop should say $row2. $row is being overwritten. Both variables should be named different from each other.

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

#3


0  

And that?:

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

#1


0  

You can acomplish that with a one single query:

您可以使用一个查询来完成它:

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

You final code should looks like:

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

<?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']; 


 }   ?>

Saludos ;)

#2


1  

Second loop should say $row2. $row is being overwritten. Both variables should be named different from each other.

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

#3


0  

And that?:

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