1

使用 php,我试图链接由相同值连接的 3 个表的结果。然后,我希望每个动态的相关结果集在页面上作为 while 循环重复。这是我想要的结果:

艺术家->

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

任何帮助将不胜感激,因为我已经为此工作了几天!

4

4 回答 4

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>
于 2013-07-18T00:21:19.850 回答
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的,以便它被缓存。抱歉,如果格式难以阅读。使用滚动条。

于 2013-07-18T01:20:29.797 回答
0

您错过了为其他人编写的 while 循环$row_artistrecordset,请查看您的代码,只有两个循环。

于 2013-07-17T23:56:57.490 回答
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 扩展现在已弃用,但在不更改所有内容且它不是 likejust add a i转换的情况下,您可以尝试将其作为临时解决方案。

于 2013-07-18T00:05:03.163 回答