Php MySQL只返回一个值

Php MySQL只返回一个值,php,mysql,sql-order-by,Php,Mysql,Sql Order By,近两个小时来,我一直在想到底出了什么问题 我已经为我的网站做了一个墙贴功能,帖子应该显示在个人资料墙上,但是不是所有的帖子都显示,只有1个是。我不知道我的代码有什么问题,我找不到任何问题 $profile\u username变量已在以前的代码和工作中定义。即使我只是在其中键入配置文件的名称,它仍然只返回一篇文章 //----------submit wall post----------// $getwallposts = mysql_query("SELECT * FROM `wallpos

近两个小时来,我一直在想到底出了什么问题

我已经为我的网站做了一个墙贴功能,帖子应该显示在个人资料墙上,但是不是所有的帖子都显示,只有1个是。我不知道我的代码有什么问题,我找不到任何问题

$profile\u username变量已在以前的代码和工作中定义。即使我只是在其中键入配置文件的名称,它仍然只返回一篇文章

//----------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'>&bull; $wallpostdate</div> 
        </div>
        <div id='wallpost-content'>
            $wallpost
        </div>
    </div>
    ";
}
/------------提交墙柱----------//
$getwallposts=mysql\u查询(“按`posted``顺序从`wallposts`中`postedto`='$profile\u username`选择*”;
//检查是否返回了任何行
而($wallposts=mysql\u fetch\u assoc($getwallposts)){
$postid=$wallposts['postid'];
$postedby_username=$wallposts['postedby'];
$wallpostdate=$wallposts['dateposted'];
$wallpost=$wallposts['post'];
$querypostedby\u info=mysql\u查询(“从`users`中选择*,其中`username`='$postedby\u username'”);
//获取上面的信息
if(mysql_num_rows($querypostedby_info)>0){
$getpostedby_info=mysql_fetch_assoc($querypostedby_info);
$postedby_id=$getpostedby_info['id'];
$postedby_profilepicture=$getpostedby_info['profilepicture'];
}
//让我们保持分线
$wallpost=nl2br($wallpost);
//显示帖子
$wallpoststicker=
"
说:
&bull;$wallpostdate
$wallpost
";
}

另外,我知道mysql已经被弃用了;不过,我的PHP版本可以很好地处理mysql查询。

在哪里回显输出? 如果在While之后,您将只获得存储在变量中的最后一个结果


while循环中的输出。

在哪里回显输出? 如果在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'>&bull; $wallpostdate</div> 
    </div>
    <div id='wallpost-content'>
        $wallpost
    </div>
</div>
";
$wallpoststicker=
"
说:
&bull;$wallpostdate
$wallpost
";
此代码覆盖每个循环上的$wallpoststicker变量。这就是为什么在最后只打印一个结果


但第二,您在循环中执行查询,这可能非常低效。你有没有想过在“Wallpost”和“users”之间做一个左连接,这样在开始循环之前就有了所需的所有数据

我可以看到该代码存在一些问题。首先,你的问题是什么:

$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'>&bull; $wallpostdate</div> 
    </div>
    <div id='wallpost-content'>
        $wallpost
    </div>
</div>
";
$wallpoststicker=
"
说:
&bull;$wallpostdate
$wallpost
";
此代码覆盖每个循环上的$wallpoststicker变量。这就是为什么在最后只打印一个结果


但第二,您在循环中执行查询,这可能非常低效。你有没有想过在“Wallpost”和“users”之间做一个左连接,这样在开始循环之前就有了所需的所有数据

再次您好:)如果您运行
while($wallposts=mysql\u fetch\u assoc($getwallposts)){print\r($wallposts);}
?数组([posted]=>3[post]=>让我们再次测试[postedto]=>Harry[postedby]=>PLAYERBOMB[dateposted]=>2016年2月14日)数组([posted]=>2[post]=>他们在这里测试[postedto]=>Harry[postedby]=>PLAYERBOMB[dateposted]=>2016年2月14日)数组([postid]=>1[post]=>嘿,这是一篇文章。这里有一个新段落[postedto]=>Harry[postedby]=>PLAYERBOMB[dateposted]=>2016年2月14日)嗨:)如果运行
while($wallposts=mysql\u fetch\u assoc($getwallposts)){print\r($wallposts);}
数组([posted]=>3[post]=>让我们再次测试[postedto]=>Harry[postedby]=>PLAYERBOMB[dateposted]=>2016年2月14日)数组([posted]=>2[post]=>Hey test here[postedto]=>Harry[postedby]=>PLAYERBOMB[dateposted]=>2016年2月14日)数组([posted]=>1[post]=>嘿,这是一篇帖子。这是一个新段落[postedto]=>Harry[postedby]=>PLAYERBOMB[dateposted]=>2016年2月14日)啊,它在另一个文件上进行了回显,我将此代码文件包含在它所回显的文件的顶部,稍后我将对其进行排序。使用$wallpoststicker.=附加到变量字符串。然后问题就解决了。声明$wallpoststicker=“”;在while.ah之前,它在另一个文件上进行了回显,我将此代码文件包含在它所回显的文件的顶部,稍后我将对其进行排序。使用$wallpoststicker.=附加到变量字符串。然后问题就解决了。声明$wallpoststicker=“”;之前。@Harry Awesome!!!我已经将你的代码加载到我的IDE中,@Harry So,对不起,你的MySQL问题现在完全解决了吗?还是仅仅解决了“$wallposttick”?当然,我会给你发电子邮件。@Harry Awesome!!!我已经将你的代码加载到我的IDE中,@Harry So,对不起,你的MySQL问题现在完全解决了吗?还是仅仅解决了问题“$wallpoststicker”?好的,我马上给你发电子邮件。