![](/img/trans.png)
[英]PHP get MYSQL id from on table and foreach inside another table
[英]FOREACH sql table entry get result from another table
我正在使用vBulletin論壇,我試圖從一個表中獲取結果,如果它存在於另一個表中。
這是數據來源的表的創建
$db->query("ALTER TABLE " . TABLE_PREFIX . "drc_settings ADD thread_ids varchar(50) NOT NULL default '0'");
我創建了一個輸入字段,用於逗號分隔的數字(線程ID)。 提交到此列,因此此列中的結果如下所示:
1,46,23
這個表中的另一列是threadid,這是帖子提交的帖子。
所以這就是我們在drc_settings表中的內容
threadid thread_ids
5 1,46,23
現在另一個存在的表是thread,在我們的線程表中:
threadid title
46 Some Title
我需要知道的是,如果thread_ids或drc_settings與線程的threadid匹配,如何從線程獲取標題?
這就是我所處的位置,但我對這個問題的看法有些偏差。
$res = $post['thread_ids'];
$res = explode (',', $res);
$post['drcid'] = '<ul>';
foreach ( $res as $ret ) {
$db->query("
SELECT
thread.threadid, drc.threadid AS threadid,
thread.threadid, thread.title AS threadtitle
FROM " . TABLE_PREFIX . "thread AS thread
LEFT JOIN " . TABLE_PREFIX . "drc_settings AS drc ON (drc.threadid=thread.threadid)
WHERE thread.threadid VAR (" . $ret . ")
");
$post['drcid'] .= '<li><a href="showthread.php?t=' . $ret . '">'. $thread[threadtitle] .'</a></li>';
}
$post['drcid'] .= '</ul>';
如果我從foreach中刪除查詢,我可以獲得$ res返回1 46 23,這讓我覺得查詢無法正常工作。
最終結果將是輸入IE中指定的每個線程的鏈接列表:
這是我當前設置返回的錯誤
Database error in vBulletin 3.8.9:
Invalid SQL:
SELECT
thread.threadid, drc.threadid AS threadid,
thread.threadid, thread.title AS threadtitle
FROM thread AS thread
LEFT JOIN drc_settings AS drc ON (drc.threadid=thread.threadid)
WHERE thread.threadid VAR (1);
MySQL Error : You have an error in your SQL syntax;
使用$ ret(父級線程)...
$query="SELECT B.threadid,B.title
FROM drc.settings A
LEFT JOIN thread B
ON FIND_IN_SET(B.threadid,A.threadids)
WHERE A.threadid={$ret};"
然后根據需要循環遍歷結果集以構建$ post數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.