而在PHP中使用MySQL的另一个内部

时间:2011-10-16 11:56:14

标签: php mysql while-loop

我需要执行以下操作 - 从数据库中获取所有艺术家的列表并将其打印在页面上。但除此之外,我还需要对“专辑”表格进行另一次查询,并获取每位艺术家的专辑,并在每位艺术家描述下打印图像。我写的代码如下:

require('db.php');

$query = "SELECT * FROM artists";
$result = mysql_query($query);

$artist_id = $result[id];
$artist_name = $result[name];
$artist_surname = $result[surname];
$artist_email = $result[email];
$artist_about = $result[about];
$artist_photo = $result[photo];
while(list($artist_id, $artist_name, $artist_surname, $artist_email, $artist_about, $artist_photo) = mysql_fetch_row($result)) :
    print "<div class='row'>";
    print "<div class='artists_left'>";  
    print  "<div class='gallery_cont'><a href='#'><img src='timthumb.php?src=images/artists/$artist_photo&w=240&h=318' alt='$artist_name' /></a></div>";
    print  "</div>";  
    print "<div class='artists_right'>";
    print   "<div class='artist_title_cont'><span class='model'>Художник:</span><span class='name'>$artist_name $artist_surname</span><span class='mypage'>Личная почта:</span><a href='#' class='mypage'>$artist_email</a></div>";
    print   "<div class='artist_title_cont' style='margin-top:20px;'><span class='name'>$artist_about</span></div>";
    print     "<ul class='artists'>";
    $albums_query = "SELECT id, title_photo, dirname FROM albums WHERE master = $artist_id";
    $albums_result = mysql_query($albums_query);
    $album_id = $albums_result[id];
    $album_photo = $albums_result[title_photo];
    $album_dirname = $albums_result[dirname];
    while(list($album_id, $album_photo, $album_dirname) = mysql_fetch_row($result)) :   
        print "<li><a href='#'><img src='galleries/$album_dirname/$album_photo' alt='image' /></a></li>";
    endwhile;
    print "</ul>";
    print      "<a href='#' class='seemore_portfolio'>все работы мастера &gt;</a>";
    print  "</div>";    
    print "</div>";
endwhile;
mysql_close($connect);

外部查询工作正常,但内部查询 - 没有。有人能帮我解决这个问题吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

将第二个$result更改为$albums_result

你也可以考虑写这样的全部内容......

<? 
        require('db.php');

        $query = "SELECT * FROM artists";
        $result = mysql_query($query);

        while(list($artist_id, $artist_name, $artist_surname, $artist_email, $artist_about, $artist_photo) = mysql_fetch_row($result))
        {

        $li = '';

        $albums_query = "SELECT id, title_photo, dirname FROM albums WHERE master = $artist_id";
        $albums_result = mysql_query($albums_query);

        while(list($album_id, $album_photo, $album_dirname) = mysql_fetch_row($albums_result))
        {
            $li .= "<li><a href='#'><img src='galleries/$album_dirname/$album_photo' alt='image' /></a></li>";
        }

        echo "<div class='row'>
        <div class='artists_left'>  
        <div class='gallery_cont'><a href='#'><img src='timthumb.php?src=images/artists/$artist_photo&w=240&h=318' alt='$artist_name' /></a></div>
        </div>  
        <div class='artists_right'>
        <div class='artist_title_cont'><span class='model'>Художник:</span><span class='name'>$artist_name $artist_surname</span><span class='mypage'>Личная почта:</span><a href='#' class='mypage'>$artist_email</a></div>
        <div class='artist_title_cont' style='margin-top:20px;'><span class='name'>$artist_about</span></div>
        <ul class='artists'>
        $li
        </ul>
        <a href='#' class='seemore_portfolio'>все работы мастера &gt;</a>
        </div>
        </div>";

        }