2015-04-01 118 views
-1

我一直在努力解決我的問題,因爲早上:)MYSQL INNER JOIN語法錯誤

我有兩個表。 table1 =發佈table2 =用戶。現在我需要從兩個表中提取數據。搜索如何使用INNER JOIN,找到一些代碼。我跑裏面phpMyAdmin的查詢,並在這裏工作得很好是我的SQL查詢

SELECT post.topic_id, post.category_id, post.topic_id, post.post_creator, post.post_date, post.post_content, users.username, users.gender, users.id FROM post INNER JOIN users ON post.post_creator=users.id WHERE post.post_creator=users.id and post.topic_id=19 ORDER BY post.post_date DESC

,但是當我用我的PHP這裏面的SQL查詢它給了我錯誤

你有一個錯誤的SQL語法;檢查對應於你的MySQL服務器版本使用附近的1號線

下面

「INNER JOIN用戶ON post.post_creator = users.id ORDER BY post.post_date ASC」正確的語法手冊是我的PHP代碼

<?php 
      include_once './forum_Scripts/connect_to_MySql.php'; 
      $cid = $_GET['cid']; 
       if(isset($_SESSION['uid'])){ 
        $logged = " | <a href ='create_topic.php?cid=".$cid."'>Click here to create a new topic</a> "; 
       }else{ 
        $logged = " | Please log in to create topics in this forum."; 
       } 

      $tid = $_GET['tid']; 
      $sql = "SELECT * FROM topics WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1"; 
      $res = mysql_query($sql) or die(mysql_error()); 
      if(mysql_num_rows($res) == 1){ 

       echo "<table width='100%'> "; 
       if(isset($_SESSION['uid'])){ 
        echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location = 'post_reply.php?cid=".$cid."&tid=".$tid."'\" /> | <a href = 'http://amaforum.net63.net/'>Return to Forum Index</a><hr />"; 
       }else{ 

       echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td>";} 

        while ($row = mysql_fetch_assoc($res)){ 
         $sql2 = "SELECT post.topic_id, post.category_id, post.topic_id, post.post_creator, post.post_date, post.post_content, users.username, users.gender, users.id" 
           . "FROM post INNER JOIN users ON post.post_creator=users.id ORDER BY post.post_date ASC" ; 
         $res2 = mysql_query($sql2) or die(mysql_error()); 
         while ($row2 = mysql_fetch_assoc($res2)) 
         { 
          echo "<tr><td valign='top' style='border:2px solid #000000'><div style='min-height: 125px;'>".$row['topic_title']."<br /> 
           by ".$row2['post_creator']." - ".$row2['post_date']."<hr />".$row2['post_content']."</div></td>" 
            . "<td width='200' valign='top' style='border: 1px solid #000000;'>" 
            . "<input id='".d_text."' type='".text."' name='".the_creator."' value='".$row2['post_creator']."' >" 
            . "echo '$user_info' " 
            . "</td></tr>" 
            . "<tr><td colspan='2'><hr /></td></tr> "; 
         } 
        $old_views = $row['topic_views']; 
        $new_views = $old_views + 1; 

        $sql3 = "UPDATE topics SET topic_views='".$new_views."' WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1 "; 
        $res3 = mysql_query($sql3) or die (mysql_error()); 
        } 
        echo "</table>"; 
       }else{ 
       echo "<p>This topic does not exist</p>"; 

      } 
      mysql_close(); 
      ?> 

我可以讓它提前工作,我真的需要從你們的幫助..

感謝

+0

這裏需要一個空格:'users.id''爲'users.id''特別是:'$ sql2 =「選擇post.topic_id,post.category_id,post.topic_id,post.post_creator,post.post_date, post.post_content,users.username,users.gender,users.id「 。 「FROM post INNER JOIN users on post.post_creator = users.id ORDER BY post.post_date ASC」;' – xQbert 2015-04-01 14:57:53

+0

您在查詢中缺少空格:'users.idFROM' – 2015-04-01 14:57:59

+0

''users.id'後面提供一些空格' – 2015-04-01 14:58:01

回答

0

這應該解決您的問題(我剛補充說,失蹤的空間什麼時候你串接您的SQL查詢的兩行):

<?php 
      include_once './forum_Scripts/connect_to_MySql.php'; 
      $cid = $_GET['cid']; 
       if(isset($_SESSION['uid'])){ 
        $logged = " | <a href ='create_topic.php?cid=".$cid."'>Click here to create a new topic</a> "; 
       }else{ 
        $logged = " | Please log in to create topics in this forum."; 
       } 

      $tid = $_GET['tid']; 
      $sql = "SELECT * FROM topics WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1"; 
      $res = mysql_query($sql) or die(mysql_error()); 
      if(mysql_num_rows($res) == 1){ 

       echo "<table width='100%'> "; 
       if(isset($_SESSION['uid'])){ 
        echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location = 'post_reply.php?cid=".$cid."&tid=".$tid."'\" /> | <a href = 'http://amaforum.net63.net/'>Return to Forum Index</a><hr />"; 
       }else{ 

       echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td>";} 

        while ($row = mysql_fetch_assoc($res)){ 
         $sql2 = "SELECT post.topic_id, post.category_id, post.topic_id, post.post_creator, post.post_date, post.post_content, users.username, users.gender, users.id " 
           . "FROM post INNER JOIN users ON post.post_creator=users.id ORDER BY post.post_date ASC" ; 
         $res2 = mysql_query($sql2) or die(mysql_error()); 
         while ($row2 = mysql_fetch_assoc($res2)) 
         { 
          echo "<tr><td valign='top' style='border:2px solid #000000'><div style='min-height: 125px;'>".$row['topic_title']."<br /> 
           by ".$row2['post_creator']." - ".$row2['post_date']."<hr />".$row2['post_content']."</div></td>" 
            . "<td width='200' valign='top' style='border: 1px solid #000000;'>" 
            . "<input id='".d_text."' type='".text."' name='".the_creator."' value='".$row2['post_creator']."' >" 
            . "echo '$user_info' " 
            . "</td></tr>" 
            . "<tr><td colspan='2'><hr /></td></tr> "; 
         } 
        $old_views = $row['topic_views']; 
        $new_views = $old_views + 1; 

        $sql3 = "UPDATE topics SET topic_views='".$new_views."' WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1 "; 
        $res3 = mysql_query($sql3) or die (mysql_error()); 
        } 
        echo "</table>"; 
       }else{ 
       echo "<p>This topic does not exist</p>"; 

      } 
      mysql_close(); 
      ?> 

要避免這樣的問題,在未來,它總是好的做法來記錄就是BEING在你的代碼的某個地方執行的SQL,甚至只是呼應它拖到網頁,而測試。這樣你可以很快地調試這樣的問題。

+1

謝謝@ xQbert西蒙伍爾夫Benoit加雷特和Abhik Chakraborty ..它現在工作正常.. – pandoy 2015-04-01 15:44:00