while 在 PHP 中使用 MySQL 的 while 内部
我需要执行以下操作 - 从数据库中获取所有艺术家的列表并将其打印在页面上。但除此之外,我需要对“专辑”表进行另一个查询并获取每个艺术家的专辑并打印每个艺术家描述下的图像。我编写的代码如下:
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);
外部查询工作正常,但内部查询却不行。有人能帮我解决这个问题吗?
提前致谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将第二个
$result
更改为$albums_result
您也可以考虑像这样编写整个内容...
Change the second
$result
to$albums_result
You can also consider writing the whole thing like this...