我一直在尝试使用 PHP 显示 MySql NOTES 表中与 QUOTES 表中的特定 NAME 匹配的所有结果。目标是我想显示每个特定名称下列出的所有评论。
QUOTES 表列:ID/NAME/EMAIL/PRICE
NOTES 表列:ID/NAME/AGENT/DATE/COMMENTS
这是我正在使用的 MySql 代码:
mysql_select_db($database_dbConnect, $dbConnect);
$query_rsquotes = "SELECT * FROM quotes ORDER BY `id` DESC";
$rsquotes = mysql_query($query_rsquotes, $dbConnect) or die(mysql_error());
$row_rsquotes = mysql_fetch_assoc($rsquotes);
$totalRows_rsquotes = mysql_num_rows($rsquotes);
mysql_select_db($database_dbConnect, $dbConnect);
$query_rsnotes = "SELECT quotes.*, notes.* FROM quotes INNER JOIN notes ON quotes.name = notes.name ORDER BY notes.date DESC";
$rsnotes = mysql_query($query_rsnotes, $dbConnect) or die(mysql_error());
$row_rsnotes = mysql_fetch_assoc($rsnotes);
$totalRows_rsnotes = mysql_num_rows($rsnotes);
然后显示结果:
<?php do { ?>
<?php echo $row_rsquotes['name']; ?>
<?php echo $row_rsquotes['email']; ?>
<?php
if ($row_rsquotes['name'] == $row_rsnotes['name']) {
do {
if ($row_rsquotes['name'] == $row_rsnotes['name']) {
echo "<div class='agentNotes'><p><strong>DATE: </strong><span class='meta-date'>" . $row_rsnotes['date'] . "</span></p><p><strong>AGENT:</strong> " . $row_rsnotes['agent'] . "</p><p><strong>AGENT NOTES:</strong> " . $row_rsnotes['comments'] . "</p></div>";
}
} while ($row_rsnotes = mysql_fetch_assoc($rsnotes));
}
?>
<?php } while ($row_rsquotes = mysql_fetch_assoc($rsquotes)); ?>
它循环并显示该特定名称的匹配记录,但当它到达下一个名称时,它不会显示匹配的注释。我做错了什么?
最佳答案
query names_table containing Names
foreach(Names as Name){
Query comment_table for comments that relates or maps Name
foreach(comments as comment){
display comment
}
}
关于php - MySql关系型数据库困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44687876/