2010-08-31 95 views
1

我有一個帶有用戶名,密碼和屬性名稱的mysql表,它允許用戶登錄到一個安全的網站部分。現在我想要做的就是從第二個表中拉入pdf,在第二個表中檢查表1中的屬性名稱與表2中pdf文檔的documentCategory,然後根據日誌記錄僅顯示相關的pdf在用戶。 (希望這是有道理的)PHP/MySQL加入2個表

表結構:

Table 1 - 
userid password property 

Table 2 - 
pdfFile documentCategory 

Where table1.property = table2.documentCategory 

我如何能得到在PHP這個工作任何幫助將不勝感激,因爲我現在完全喪失。
S

回答

0
select t2.pdfFile 
from t1 inner join t2 
on t1.property = t2.documentCategory 
where t1.userId = $userId 

更新...不知道這是一個PHP問題...更像是一個SQL問題。

+0

謝謝大家的幫助 - 非常感謝:) 結束了得到這個工作: $用戶id = $ logResult [「用戶id」]; $ query = mysql_query(「select * from table1 inner join table2 on table1 .property = table2.documentCategory where table1.userid ='$ userid'」); $ result = mysql_fetch_array($ query); \t \t \t \t echo'

'。$ result ['documentFilename']。'

'。 PHP_EOL; \t \t \t \t \t \t echo'

'。$ result ['documentCategory']。'

'。 PHP_EOL; – ss888 2010-09-01 08:26:02

0

SELECT table2.pdfFile,table2.documentCategory FROM table2,table1 WHERE table1.property = table2.documentCategory AND table1.user_id =?

確保table1.user_id具有PRIMARY索引,而table2.documentCategory也具有索引。

4

如果用戶已經登錄(也就是,你有userid)您的SQL查詢可能會是這樣的:

$query = 'SELECT pdfFile 
    FROM table2 
    INNER JOIN table1 ON property = documentCategory 
    WHERE table1.userid = '.intval($userid); 

$result = mysql_query($query); 

適應這爲您的首選數據庫抽象層。