[英]PHP get all matching rows in relational mysql innodb database
我在數據庫中建立了3個具有多對多關系的表。 對於數據庫中的$refernece_keys
變量,應該返回數據庫中的所有相關字段-但它僅返回第一個分配的字段,而忽略其他字段。
eg page1 should return keys_href: id's 1, 2, 3, 4, 8, 10, 15 - however it only returns id:1 page2 should return keys href: id's 3, 4, 5, 7, 8, 11 - however it only returns id:3
我的陣列有問題嗎?
$SQL =
"SELECT * FROM learn_more AS lm
LEFT JOIN learn_more_to_reference_key AS lmtrk
ON lm.id = lmtrk.learn_more_id
LEFT JOIN reference_keys AS rk
ON rk.keys_id = lmtrk.reference_key_id
WHERE lm.page_title = '$this_page'";
$result = mysql_query($SQL) or die(mysql_error());
while ($db_field = mysql_fetch_array($result))
{
$id = $db_field['id'];
$main_title = $db_field['main_title'];
$main_content = $db_field['main_content'];
$reference_keys = $db_field['keys_href'];
$sub_title = $db_field['sub_title'];
$sub_content = $db_field['sub_content'];
}
------------------------------------------------>編輯
我的PHP視圖頁面是一個模板
<div id="content">
<div class="section_frame">
<div class="section_title">
<?php echo $main_title; ?>
</div>
<div class="section_content">
<?php echo $main_content; ?>
</div>
<div class="section_content_ref">
<span class="hl_reference"><u>key references:</u></span>
<?php echo $reference_keys; ?>
</div>
<div class="sub_section_title">
<?php echo $sub_title; ?>
</div>
<div class="sub_section_content">
<?php echo $sub_content; ?>
</div>
所以基本上我只希望我的價值觀能得到回應。
嗯,我首先想到的是,由於您的while循環會遍歷所有記錄,因此在完成之后,只有最后一條記錄才可用...除了您只看到第一個記錄。 如果要在while循環中添加echo $id
,是否會打印所有ID? 另外,如果您還有更多代碼來完成該示例,則將對您有所幫助。
$items = array();
while ($db_field = mysql_fetch_array($result))
{
$id = $db_field['id'];
$main_title = $db_field['main_title'];
$main_content = $db_field['main_content'];
$reference_keys = $db_field['keys_href'];
$sub_title = $db_field['sub_title'];
$sub_content = $db_field['sub_content'];
$items[$id] = array($main_title, $main_content, $reference_keys, $sub_title, $sub_content);
}
上面將創建一個數組,其中每個記錄的ID指向記錄信息。 這是否符合您的目標?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.