所以我的数据库中有两个表,一个叫做用户,另一个是新闻 我认为用户可以在网站上添加新闻帖,但我无法在帖子旁边显示用户的图像
这是我现在的代码
<?php
$News = "";
$user_id = "";
$sqlCommand = "SELECT * FROM News ORDER BY id DESC LIMIT 10";
$sqlCommand3 = "SELECT * FROM Users";
$query = mysql_query($sqlCommand) or die(mysql_error());
$query3 = mysql_query($sqlCommand3) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$News .= "";
while(($row = mysql_fetch_array($query)) && ($row2 = mysql_fetch_array($query3)) ){
$News .= "<a href=\"news?id=".$row['id']."\"><div class=\"news-post\"> <img src=\".$row2['author_avatar']."\"><p>".$row['author']."</p> <h2>".$row['title']."</h2></div> </a>";
} // close while
} else {
$News = "No News!";
}
?>
我想在哪里说$ row2 ['author_avatar']来回显用户表中的图片
答案 0 :(得分:0)
你在$ row2 ['author_avatar']之前错过了一个演讲标记(“)。
<?php
$News = "";
$user_id = "";
$sqlCommand = "SELECT * FROM News ORDER BY id DESC LIMIT 10";
$sqlCommand3 = "SELECT * FROM Users";
$query = mysql_query($sqlCommand) or die(mysql_error());
$query3 = mysql_query($sqlCommand3) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$News .= "";
while(($row = mysql_fetch_array($query)) && ($row2 = mysql_fetch_array($query3)) ){
$News .= "<a href=\"news?id=".$row['id']."\"><div class=\"news-post\"> <img src="\".$row2['author_avatar']."\"><p>".$row['author']."</p> <h2>".$row['title']."</h2></div> </a>";
} // close while
} else {
$News = "No News!";
}
?>
你应该查找INNER JOIN而不是像andrewsi所提到的那样有两个SQL查询。这是一个关于如何使用它的好教程https://www.youtube.com/watch?v=6BfofgkrI3Y。