MySQL 只返回一个值
MySQL only returning one value
近两个小时以来一直在努力找出问题所在。
我为我的网站制作了墙 post 功能,post 应该显示在个人资料墙上,但是没有显示所有 post,只有1是。我不知道我的代码有什么问题,我找不到任何问题。
$profile_username 变量已在前面的代码中定义并且有效。即使我只是在其中输入配置文件的名称,它仍然只有 returns 个 post.
//----------submit wall post----------//
$getwallposts = mysql_query("SELECT * FROM `wallposts` WHERE `postedto`='$profile_username' ORDER BY `postid`");
//check if any rows are returned
while($wallposts = mysql_fetch_assoc($getwallposts)) {
$postid = $wallposts['postid'];
$postedby_username = $wallposts['postedby'];
$wallpostdate = $wallposts['dateposted'];
$wallpost = $wallposts['post'];
$querypostedby_info = mysql_query("SELECT * FROM `users` WHERE `username`='$postedby_username'");
//get the info above
if (mysql_num_rows($querypostedby_info) > 0) {
$getpostedby_info = mysql_fetch_assoc($querypostedby_info);
$postedby_id = $getpostedby_info['id'];
$postedby_profilepicture = $getpostedby_info['profilepicture'];
}
//lets keep the line breaks
$wallpost=nl2br($wallpost);
//display the posts
$wallpoststicker =
"
<div id='wallpost-container'>
<div id='wallpost-header'>
<img src='$postedby_profilepicture'><div id='wallpost-header-by'><a href='/profile.php?id=$postedby_id'>$postedby_username</a> said:</div>
<div id='wallpost-date'>• $wallpostdate</div>
</div>
<div id='wallpost-content'>
$wallpost
</div>
</div>
";
}
此外,我知道 mysql 已被弃用;我的 PHP 版本可以很好地处理 mysql 查询。
你在哪里回显你的输出?
如果它在 While 之后,您将只会获得存储在变量中的最后一个结果。
while 循环内的输出。
我发现该代码存在一些问题。首先,您的问题似乎是什么:
$wallpoststicker =
"
<div id='wallpost-container'>
<div id='wallpost-header'>
<img src='$postedby_profilepicture'><div id='wallpost-header-by'><a href='/profile.php?id=$postedby_id'>$postedby_username</a> said:</div>
<div id='wallpost-date'>• $wallpostdate</div>
</div>
<div id='wallpost-content'>
$wallpost
</div>
</div>
";
此代码在每个循环中覆盖 $wallpoststicker 变量。这就是为什么你只在最后打印一个结果。
但其次,您在循环中执行查询,这可能效率很低。您是否考虑过在 "wallposts" 和 "users" 之间执行 LEFT JOIN,以便在开始循环之前获得所需的所有数据?
近两个小时以来一直在努力找出问题所在。
我为我的网站制作了墙 post 功能,post 应该显示在个人资料墙上,但是没有显示所有 post,只有1是。我不知道我的代码有什么问题,我找不到任何问题。
$profile_username 变量已在前面的代码中定义并且有效。即使我只是在其中输入配置文件的名称,它仍然只有 returns 个 post.
//----------submit wall post----------//
$getwallposts = mysql_query("SELECT * FROM `wallposts` WHERE `postedto`='$profile_username' ORDER BY `postid`");
//check if any rows are returned
while($wallposts = mysql_fetch_assoc($getwallposts)) {
$postid = $wallposts['postid'];
$postedby_username = $wallposts['postedby'];
$wallpostdate = $wallposts['dateposted'];
$wallpost = $wallposts['post'];
$querypostedby_info = mysql_query("SELECT * FROM `users` WHERE `username`='$postedby_username'");
//get the info above
if (mysql_num_rows($querypostedby_info) > 0) {
$getpostedby_info = mysql_fetch_assoc($querypostedby_info);
$postedby_id = $getpostedby_info['id'];
$postedby_profilepicture = $getpostedby_info['profilepicture'];
}
//lets keep the line breaks
$wallpost=nl2br($wallpost);
//display the posts
$wallpoststicker =
"
<div id='wallpost-container'>
<div id='wallpost-header'>
<img src='$postedby_profilepicture'><div id='wallpost-header-by'><a href='/profile.php?id=$postedby_id'>$postedby_username</a> said:</div>
<div id='wallpost-date'>• $wallpostdate</div>
</div>
<div id='wallpost-content'>
$wallpost
</div>
</div>
";
}
此外,我知道 mysql 已被弃用;我的 PHP 版本可以很好地处理 mysql 查询。
你在哪里回显你的输出? 如果它在 While 之后,您将只会获得存储在变量中的最后一个结果。
while 循环内的输出。
我发现该代码存在一些问题。首先,您的问题似乎是什么:
$wallpoststicker =
"
<div id='wallpost-container'>
<div id='wallpost-header'>
<img src='$postedby_profilepicture'><div id='wallpost-header-by'><a href='/profile.php?id=$postedby_id'>$postedby_username</a> said:</div>
<div id='wallpost-date'>• $wallpostdate</div>
</div>
<div id='wallpost-content'>
$wallpost
</div>
</div>
";
此代码在每个循环中覆盖 $wallpoststicker 变量。这就是为什么你只在最后打印一个结果。
但其次,您在循环中执行查询,这可能效率很低。您是否考虑过在 "wallposts" 和 "users" 之间执行 LEFT JOIN,以便在开始循环之前获得所需的所有数据?