php - MYSQL INNER JOIN syntax error -


i have been trying fix issue on since morning :)

i have 2 tables. table1= post table2=users. need extract data both tables. searched how use inner join, found codes. run query inside phpmyadmin , works fine here sql query

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 post inner join users on post.post_creator=users.id post.post_creator=users.id , post.topic_id=19 order post.post_date desc

but when use sql query inside php gives me error

you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'inner join users on post.post_creator=users.id order post.post_date asc' @ line 1

below php code

<?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 create new topic</a> ";                 }else{                     $logged = " | please log in create topics in forum.";                 }               $tid = $_get['tid'];              $sql = "select * topics category_id='".$cid."' , 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 forum index</a><hr />";                  }else{                   echo "<tr><td colspan='2'><p>please log in add 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 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 />                                  ".$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."' category_id='".$cid."' , id='".$tid."' limit 1 ";                      $res3 = mysql_query($sql3) or die (mysql_error());                      }                      echo "</table>";                  }else{                  echo "<p>this topic not exist</p>";               }              mysql_close();              ?> 

i can work , need guys..

thanks in advance

this should solve problem (i've added space missing when concatenated 2 lines of sql query):

<?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 create new topic</a> ";                 }else{                     $logged = " | please log in create topics in forum.";                 }               $tid = $_get['tid'];              $sql = "select * topics category_id='".$cid."' , 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 forum index</a><hr />";                  }else{                   echo "<tr><td colspan='2'><p>please log in add 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 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 />                                  ".$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."' category_id='".$cid."' , id='".$tid."' limit 1 ";                      $res3 = mysql_query($sql3) or die (mysql_error());                      }                      echo "</table>";                  }else{                  echo "<p>this topic not exist</p>";               }              mysql_close();              ?> 

to avoid issues in future, it's practice log sql that's being executed in code somewhere, or echo onto web page while testing. way you'll debug issues this.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -