MYSQL INNER JOIN 语法错误

MYSQL INNER JOIN syntax error

从早上开始我就一直在努力解决我的问题:)

我有两张桌子。 table1= post 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 查询时,它给我错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN users ON post.post_creator=users.id ORDER BY post.post_date ASC' at line 1

下面是我的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();
             ?>

我可以让它工作,我真的需要你们的帮助..

提前致谢

这应该可以解决您的问题(我刚刚添加了您连接 sql 查询的两行时丢失的 space):

<?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();
             ?>

为避免将来出现此类问题,最好在某处记录代码中正在执行的 sql,甚至在测试时将其回显到网页上。这样您就可以非常快速地调试此类问题。