使用php,我试图链接3个由相同值连接的表的结果。然后,我希望每个动态的相关结果集在页面上作为while循环重复。这是我想要的结果:
artist->
series1-> piece1,piece2
series2-> piece3,piece4
艺术家和系列表共享一个名为“艺术家”的匹配列。系列和零件表具有匹配的列名称“系列”。我知道这些表通过数据库中相同的匹配值链接,而另一个页面级联删除正在工作。
目前它只将系列显示为回声重复循环,但两侧都没有相关的艺术家或作品。像这样:http://www.exhibitjewellery.com/artistindex.php
mysql_fetch_assoc是否正确,我不确定。我很困惑这些表是否正确链接,或者问题是我如何划分正文部分进行格式化。我有一种感觉,多维数组可能有助于甚至嵌套表格,但我还没有完全掌握所有细节如何在代码的每个部分组合。请帮忙!
PHP高于头部:
<?php
mysql_select_db($database_connectmysql, $connectmysql);
$query_artistrecordset = "SELECT * FROM artists ORDER BY artist ASC";
$artistrecordset = mysql_query($query_artistrecordset, $connectmysql) or die(mysql_error());
$row_artistrecordset = mysql_fetch_assoc($artistrecordset);
$totalRows_artistrecordset = mysql_num_rows($artistrecordset);
mysql_select_db($database_connectmysql, $connectmysql);
$query_seriesrecordset = "SELECT * FROM series, artists WHERE series.artist=artists.artist ORDER BY exhibition ASC";
$seriesrecordset = mysql_query($query_seriesrecordset, $connectmysql) or die(mysql_error());
$resultseries = mysql_query($query_seriesrecordset);
$row_seriesrecordset = mysql_fetch_assoc($resultseries);
$totalRows_seriesrecordset = mysql_num_rows($seriesrecordset);
mysql_select_db($database_connectmysql, $connectmysql);
$query_piecerecordset = "SELECT * FROM pieces,series WHERE pieces.piece=series.series ORDER BY piece ASC";
$piecerecordset = mysql_query($query_piecerecordset, $connectmysql) or die(mysql_error());
$resultpiece = mysql_query($query_piecerecordset);
$row_piecerecordset = mysql_fetch_assoc($resultpiece);
$totalRows_piecerecordset = mysql_num_rows($piecerecordset);
?>
这就是我试图在体内回应它的方式:
<div id="serieslist" align="right">
<?php echo $row_artistrecordset['artist']; ?><br />
<?php echo $row_artistrecordset['website']; ?><br />
<?php echo $row_artistrecordset['artist_statement']; ?><br />
<?php do { ?>
<?php echo $row_seriesrecordset['series']; ?><br />
<?php echo $row_seriesrecordset['exhibition']; ?><br />
<?php echo $row_seriesrecordset['series_statement']; ?><br />
<?php do { ?>
<?php echo $row_piecerecordset['piece']; ?><br />
<?php echo $row_piecerecordset['description']; ?><br />
<?php echo $row_piecerecordset['category']; ?><br />
<?php echo $row_piecerecordset['dimensions']; ?><br />
<?php echo $row_piecerecordset['price']; ?><br />
add to collection button<br />
<?php } while ($row_piecerecordset = mysql_fetch_assoc($resultpiece)); ?>
<?php } while ($row_seriesrecordset = mysql_fetch_assoc($resultseries)); ?>
</div>
</body>
</html>
<?php
mysql_free_result($artistrecordset);
mysql_free_result($seriesrecordset);
mysql_free_result($piecerecordset);
?>
任何帮助都会非常感激,因为我已经做了好几天了!
答案 0 :(得分:2)
使用您的代码,这是一个转换为mysqli的版本,删除了一些冗余行。我无法对此进行测试,因此可能需要进行一些调试。
<?php
$connectmysql = mysqli_connect("dbhost","dbuser","dbname","dbname") or die("Database error:".mysqli_connect_error);
$query_artistrecordset = "SELECT * FROM artists ORDER BY artist ASC";
$artistrecordset = mysqli_query($connectmysql, $query_artistrecordset) or die(mysqli_error);
$query_seriesrecordset = "SELECT * FROM series, artists WHERE series.artist=artists.artist ORDER BY exhibition ASC";
$seriesrecordset = mysqli_query($connectmysql, $query_seriesrecordset ) or die(mysqli_error);
$query_piecerecordset = "SELECT * FROM pieces,series WHERE pieces.piece=series.series ORDER BY piece ASC";
$piecerecordset = mysqli_query($connectmysql, $query_piecerecordset) or die(mysqli_error);
echo "<div id="serieslist" align="right">"
while ($row_artistrecordset = mysqli_fetch_assoc($artistrecordset)) {
echo $row_artistrecordset['artist'],"<br>";
echo $row_artistrecordset['website'],"<br>";
echo $row_artistrecordset['artist_statement'],"<br>";
while ($row_seriesrecordset = mysqli_fetch_assoc($seriesrecordset)) {
echo $row_seriesrecordset['series'],"<br>";
echo $row_seriesrecordset['exhibition'],"<br>";
echo $row_seriesrecordset['series_statement'],"<br>";
while ($row_piecerecordset = mysqli_fetch_assoc($piecerecordset)) {
echo $row_piecerecordset['piece'],"<br>";
echo $row_piecerecordset['description'],"<br>";
echo $row_piecerecordset['category'],"<br>";
echo $row_piecerecordset['dimensions'],"<br>";
echo $row_piecerecordset['price'],"<br>";
echo "add to collection button<br />";
} // end of pieces
} // end of series
} //end of artists
mysqli_free_result($artistrecordset);
mysqli_free_result($seriesrecordset);
mysqli_free_result($piecerecordset);
echo "</div>";
?>
</body>
</html>
答案 1 :(得分:1)
我建议您使用面向对象的PHP。将其保存在一个名为db.php的单独安全页面上,或者其他内容:
//db.php
<?php
function db(){
return new mysqli('replaceWithHostName', 'relaceWithUserName', 'replaceWithPassWord', 'replaceWithDatebaseName');
}
?>
现在换另一页:
//other.php
<?php
include('db.php'); $db = db(); $nr = 'No Results Were Found'; $od = '<div>'; $cd = '</div>'; $br = '<br />'; $ar = $sr = $pr = '';
$artistrecordset = $db->query('SELECT * FROM artists ORDER BY artist ASC');
if(!$artistrecordset)die($db->error);
if($artistrecordset->num_rows > 0){
while($row_ar = $artistrecordset->fetch_assoc()){
$ar .= $od.$row_ar['artist'].$br.$row_ar['website'].$br.$row_ar['artist_statement'].$cd;
}
$artistrecordset->free();
}
else){
die($nr);
}
$seriesrecordset = $db->query('SELECT * FROM series, artists WHERE series.artist=artists.artist ORDER BY exhibition ASC');
if(!$seriesrecordset)die($db->error);
if($seriesrecordset->num_rows > 0){
while($row_sr = $seriesrecordset->fetch_assoc()){
$sr .= $od.$row_sr['series'].$br.$row_sr['exhibition'].$br.$row_sr['series_statement'].$cd;
}
$seriesrecordset->free();
}
else){
die($nr);
}
$piecerecordset = $db->query('SELECT * FROM pieces,series WHERE pieces.piece=series.series ORDER BY piece ASC');
if(!$piecerecordset)die($db->error);
if($piecerecordset->num_rows > 0){
while($row_pr = $piecerecordset->fetch_assoc()){
$pr .= $od.$row_pr['piece'].$br.$row_pr['description'].$br.$row_pr['category'].$br.$row_pr['dimensions'].$br.$row_pr['price'].$cd;
}
$piecerecordset->free();
}
else){
die($nr);
}
$db->close();
$head = '<html><head></head><body>'; //this could be your other info
echo "$head<div id='serieslist' align='right'>$ar$sr$pr$cd".
"<script type='text/javascript'>/*you should put your JavaScript here*/</script>".
'</body></html>';
?>
实际上,您应该为JavaScript使用外部src
,以便进行缓存。对不起,如果格式难以阅读。使用滚动条。
答案 2 :(得分:0)
你错过了为$row_artistrecordset
编写while循环,就像你为其他人编写的那样,看你的代码只有两个循环。
答案 3 :(得分:0)
首先尝试使用像phpMyAdmin这样的查询来查看它是否得到你想要的结果。
SELECT *
FROM artists a
JOIN series s ON s.artist = a.artist
JOIN pieces p ON p.series = s.series
ORDER BY a.artist;
然后像这样处理单个结果。
mysql_select_db($database_connectmysql, $connectmysql);
$q = "SELECT * FROM artists a
JOIN series s ON s.artist = a.artist
JOIN pieces p ON p.series = s.series
ORDER BY a.artist";
$result = mysql_query($q, $connectmysql) or die(mysql_error());
foreach ( $row = mysql_fetch_assoc($result) ) {
echo $row['artist'] . '<br />';
echo $row['website'] . '<br />';
echo $row['artist_statement'] . '<br />';
echo $row['series'] . '<br />';
echo $row['exhibition'] . '<br />';
echo $row['series_statement'] . '<br />';
echo $row['piece'] . '<br />';
echo $row['description'] . '<br />';
echo $row['category'] . '<br />';
echo $row['dimensions'] . '<br />';
echo $row['price'] . '<br />';
echo ' add to collection button<br />';
}
好的你也应该使用mysqli或PDO,因为现在不推荐使用mysql扩展,但是如果没有改变所有内容而不是类似于just add a i
转换的内容,你可以尝试将其作为临时解决方案。