while 在 PHP 中使用 MySQL 的 while 内部

发布于 2024-12-10 08:05:59 字数 2135 浏览 0 评论 0原文

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

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);

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

提前致谢。

I need to do the following - get the list of all artists from the database and print them on a page. But besides of that, I need to make another query to the "albums" table and get the albums of each artist and print the images under each artist description. The code I've written is as follows:

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'>все работы мастера ></a>";
    print  "</div>";    
    print "</div>";
endwhile;
mysql_close($connect);

The outer query works fine, but the inner one - does not. Could anybody help me to figure this out?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠北森屋 2024-12-17 08:05:59

将第二个 $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'>все работы мастера ></a>
        </div>
        </div>";

        }

Change the second $result to $albums_result

You can also consider writing the whole thing like this...

<? 
        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'>все работы мастера ></a>
        </div>
        </div>";

        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文