2012-09-18 111 views
0
$sql = mysql_query(" 
    SELECT * 
    FROM forum_posts 
    LEFT JOIN mymembers ON forum_posts.post_author_id = mymembers.id 
    WHERE section_id='$sid' ORDER BY date_time DESC LIMIT 25 
"); 
$dynamicList = ""; 
$numRows = mysql_num_rows($sql); 

while($row = mysql_fetch_array($sql)){ 
    $reply_author_id = $row["post_author_id"]; 
    $author = $row["username"]; 
    $id = $row["id"]; 
    $anon = $row["anon"]; 
    $post_body=$row["post_body"]; 
    $post_author_id = $row["post_author_id"]; 
    $date_time = $row["date_time"]; 
    $thread_title = $row["thread_title"]; 

這是我正在執行的mysql查詢。問題是表mymembers和forum_posts都有id字段,所以當我使用$ id並保存'id'時,成員的id被存儲在其中。以下是我正在顯示的代碼mysql連接沒有正確執行

$displayList .= '<a href="view_thread1.php?id=' . $id .'"> 

'。 $ thread_title。 ''。 $ author。';

我想將$ reply_author中的成員的id和線程的id保存到$ id變量中。

有沒有一種方法可以做到這一點,而無需更改表中的列名稱。

完成更改查詢工作。現在代碼看起來像

$sql = mysql_query(
"SELECT forum_posts.post_author_id AS post_author_id, forum_posts.id AS id, mymembers.username, forum_posts.anon,forum_posts.post_body,forum_posts.thread_title, forum_posts.date_time FROM forum_posts LEFT JOIN mymembers ON forum_posts.post_author_id=mymembers.id WHERE section_id='$sid' ORDER BY date_time DESC LIMIT 25"); 
+0

您可以別名的列: 'SELECT mymembers.id AS mymember_id,forum_posts.id AS postid,...' – andrewsi

回答

3

這樣做的最佳做法是列出查詢中的所有字段。

,而不是使用SELECT *將其更改爲:

SELECT mymembers.id as id, forum_posts.id as fpID, etc... 
0

無需在數據庫中更改列名,只要你的查詢中對其進行重命名:

SELECT members.id as reply_author 
     members.username, 
     [... other membes columns listed as the above ...] 
     forum_posts.* 
FROM forum_posts 
     LEFT JOIN mymembers 
       ON forum_posts.post_author_id = mymembers.id 
WHERE section_id = '$sid' 
ORDER BY date_time DESC 
LIMIT 25