2012-06-03 67 views
1

我是PHP的初學者,但一直在論壇上嘗試一個項目。但我遇到了一個問題。PHP字符串和SQL故障

  $getLatest = dbquery("SELECT * FROM site_forum_thread_comments,site_forum_threads,site_forum_subs WHERE site_forum_thread_comments.thread_id=site_forum_threads.thread_id AND 
     site_forum_threads.sub_id=site_forum_subs.sub_id AND 
     site_forum_subs.main_id='".$forum['ID']."' 
     ORDER BY site_forum_thread_comments.timestamp DESC LIMIT 1"); 
      while ($latest = mysql_fetch_assoc($getLatest)) 
       { 
        $getUser = dbquery("SELECT * FROM users WHERE id='".$latest['user_comment_id']."'"); 
        while ($user = mysql_fetch_assoc($getUser)) 
        { 
          echo ' <BR>Latest Post: '.$latest['comment'].' 
          <BR>User: '.$user['username'].' 
          <BR>Forum: '.substr($latest['site_forum_threads.title'],0,20).'...'; 
        } 
       } 

這僅僅是一個代碼片段。會發生什麼是,$ getLatest從我的數據庫中的許多不同表中獲取來自SQL查詢的代碼。 我的問題: 在最後一部分論壇「.substry($最新[」 ... blahblahblah']

我是否能夠添加表功能呢?所以,我可以檢索site_forum_threads列‘標題’?因爲在我的其他表格中,還有一個名爲「表格」的列,並且查詢會混淆在哪一列之間應該從中獲取數據。

在此先感謝!

編輯:@zerkms

$getLatest = dbquery("SELECT * FROM site_forum_thread_comments,site_forum_threads,site_forum_subs 
     site_forum_threads.title AS title_from_site_forum_threads 
     WHERE site_forum_thread_comments.thread_id=site_forum_threads.thread_id AND 
     site_forum_threads.sub_id=site_forum_subs.sub_id AND 
     site_forum_subs.main_id='".$forum['ID']."' AND 
     side_forum_threads.title= title_from_site_forum_threads 
     ORDER BY site_forum_thread_comments.timestamp DESC LIMIT 1"); 
      while ($latest = mysql_fetch_assoc($getLatest)) 
       { 
        $getUser = dbquery("SELECT * FROM users WHERE id='".$latest['user_comment_id']."'"); 
        while ($user = mysql_fetch_assoc($getUser)) 
        { 
          echo ' <BR>Latest Post: '.$latest['comment'].' 
          <BR>User: '.$user['username'].' 
          <BR>Forum: '.substr($latest['title_from_site_forum_threads'],0,20).'...'; 
        } 
       } 

回答

1

是的,只需指定使用$latest['title_from_site_forum_threads']

明確的名稱和別名它

site_forum_threads.title AS title_from_site_forum_threads 

和訪問它在總的選擇部分可能看起來像

SELECT *, site_forum_threads.title AS title_from_site_forum_threads FROM ... 

但我個人建議你到永遠通過枚舉用逗號分隔的所有字段,只選擇必要的東西

+0

感謝您的快速響應,儘管當我添加代碼在,我得到一個錯誤:不是唯一的表/別名:'site_forum_threads'。 – zuc0001

+0

@ user1433113:我看不到結果查詢後,我不能說你做錯了什麼。把它作爲你的問題更新請 – zerkms

+0

@ user1433113:你應該把我告訴你的'SELECT'部分。請再次查看我的答案 – zerkms