我正在开发一个评论系统,其中我将要从两个表中提取信息,问题是来自第一个查询(找到的ID)的结果影响第二个查询的输出。我的桌子看起来像这样。
成员
Id,First_name,Last_name,电子邮件,密码,Img_url,Activation_No,已激活和日期
帖子
PostId,Email,Text,ForumId,DatePosted,Likes,Dislikes
我使用的代码是;
<?php
// retrive post
include('php/config.php');
include ('php/function.php');
// retrive comments with post id
$stmt = $mysqli->prepare("SELECT (posts.Email,posts.Text) FROM posts WHERE posts.ForumId = '$forumId' LEFT JOIN SELECT (members.Img_url) FROM members WHERE members.Email = (posts.Email)");
$stmt->execute();
$stmt->bind_result($PostId,$Email,$Text,$ForumId,$DatePosted,$Likes,$Dislikes,$usr_img);
$stmt->fetch();
print "<div class=comment-item>";
print "<div class=comment-avatar>";
print "<img src=".$usr_img."alt=avatar>";
print "</div>";
print "<div class=comment-post>";
print "<h4>" .$Email. "<span> said....</span></h4>";
print "<p>" .$Text. "</p>";
print "</div>";
print"</div>";
$stmt->close();
?>
我收到的错误讯息是
致命错误:在第90行的C:\ Users \ PC \ Documents \ XAMPP \ htdocs \ post.php中的非对象上调用成员函数execute()
答案 0 :(得分:1)
您的SQL查询有错误。 试试这个
SELECT posts.Email, posts.Text, members.Img_url
FROM posts
LEFT JOIN members
ON members.Email = posts.Email
WHERE posts.ForumId = '$forumId'
答案 1 :(得分:0)
您的prepare()
查询失败,因此$stmt
是非对象。看起来您的查询语法有误,因为LEFT JOIN
之后您无法WHERE
,而JOIN
使用ON
而不是WHERE
。试试这样的事情 -
$stmt = $mysqli->prepare("SELECT posts.Email,posts.Text, members.Img_url FROM posts LEFT JOIN members ON members.Email = posts.Email WHERE posts.ForumId = '$forumId'") ;
答案 2 :(得分:0)
SELECT posts.Email, posts.Text, members.Img_url
FROM posts
LEFT JOIN members ON members.Email = posts.Email
WHERE posts.ForumId = '$forumId'
你应该看看mysql的语法..你在这里犯了一些错误;) 我希望它能像这样工作。