Php MYSQL内部连接语法错误
从早上开始,我一直在努力解决我的问题:) 我有两张桌子。表1=发布表2=用户。现在我需要从两个表中提取数据。搜索了如何使用内部联接,找到了一些代码。我在phpmyadmin中运行了查询,它运行得很好这是我的sql查询Php MYSQL内部连接语法错误,php,mysql,netbeans,inner-join,Php,Mysql,Netbeans,Inner Join,从早上开始,我一直在努力解决我的问题:) 我有两张桌子。表1=发布表2=用户。现在我需要从两个表中提取数据。搜索了如何使用内部联接,找到了一些代码。我在phpmyadmin中运行了查询,它运行得很好这是我的sql查询 选择post.topic\u id、post.category\u id、post.topic\u id、post.post\u创建者、post.post\u日期、post.post\u内容、users.username、users.gender、users.id 来自post内部
选择post.topic\u id、post.category\u id、post.topic\u id、post.post\u创建者、post.post\u日期、post.post\u内容、users.username、users.gender、users.id
来自post内部加入用户
ON post.post_creator=users.id
其中post.post\u creator=users.id,post.topic\u id=19
邮购。邮购日期说明
但是,当我在php中使用这个sql查询时,它会给我带来错误
您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在第1行使用“内部加入post.post\u creator=users.id ORDER BY post.post\u 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();
?>
这应该可以解决您的问题(我刚刚添加了连接sql查询的两行时缺少的空间):
这里需要一个空格:users.id“
成为users.id”
特别是:$sql2=“选择post.topic\u id、post.category\u id、post.topic\u id、post.post\u创建者、post.post\u日期、post.post\u内容、users.username、users.gender、users.id”。“从post.post\u creator=users.id按post.post\u日期ASC排序的post内部加入用户”
您在查询中缺少一个空格:users.idFROM
在users.id“
之后提供一些空格,谢谢@xQbert Simon Woolf Benoit Garret和Abhik Chakraborty..现在运行良好。。
<?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();
?>