简体   繁体   English

MySQL JOIN 查询后显示结果 PHP

[英]Displaying results after MySQL JOIN query with PHP

$sql = "SELECT messages.text, users.name FROM messages INNER JOIN users ON messages.user_id=users.id ORDER BY messages.ms_id DESC LIMIT 10";
$result = mysql_query($sql);
$rows = array();
    while($row = mysql_fetch_array($result))
    {
            $rows[]=$row;
    }
    echo $rows[0][1].$rows[0][0];
/*         for($i=0;$i<=10;$i++)
                {

                                  echo $rows[i][1].$rows[i][0];

                }
*/

This script is supposed to show the last 10 messages in a chat.What I'm doing is getting the Name of the user from the users table and the text from the message table and I want to display them in my chat window.Right now I have only 4 messages recorded and don't know how this affects the whole script I should implement a check for this too, but the bigger problem is that when i use echo $rows[0][1].$rows[0][0];这个脚本应该在聊天中显示最后 10 条消息。我正在做的是从用户表中获取用户的名称和从消息表中获取文本,我想在我的聊天 window 中显示它们。现在我只记录了 4 条消息,不知道这对整个脚本有何影响,我也应该对此进行检查,但更大的问题是当我使用echo $rows[0][1].$rows[0][0]; the info is displayed correctly, but when I try to make a loop so I can show tha last 10 (I tried the commented one) then nothing is displayed.I thought at least when I use this loop I'll see the 4 recorded messages but what really happen is a blank window.Obvously I have the info recorded in $rows[] and can echo it, but don't understand why this loop don't work at all.I'll appreciate if someone can help me with this and with the check if the messages are less then 10.信息显示正确,但是当我尝试制作一个循环以便我可以显示最后 10 个(我尝试了评论的那个)然后什么都没有显示。我想至少当我使用这个循环时,我会看到 4 条记录的消息但真正发生的是一个空白的 window。显然我在 $rows[] 中记录了信息并且可以回显它,但不明白为什么这个循环根本不起作用。如果有人可以帮助我,我将不胜感激这并检查消息是否少于 10。

Thanks.谢谢。

Leron PS Here is the edited script, thanks to all of you, I need the array because otherwise the most recent message is shown at the top which is not an opiton when I use it for diplaying chat masseges. Leron PS 这是编辑后的脚本,感谢大家,我需要数组,否则最新消息显示在顶部,当我使用它来显示聊天按摩时,这不是一个选项。

 for($i=10;$i>=0;$i--)
            {
                      if($rows[$i][1]!="" || $rows[$i][0]!="")
                           {
                              echo $rows[$i][1].' : '.$rows[$i][0];
                              echo "</br>";
                           }
            }

Your FOR loop was running 11 times even if only 10 records.即使只有 10 条记录,您的 FOR 循环也运行了 11 次。 The second clause should be a < instead of <= .第二个子句应该是<而不是<= Plus the $ was missing on the i variable.加上 i 变量上缺少$

For example sake, you don't really need to make an array from the rows, and you can refer to the fields by name:例如,您实际上并不需要从行中创建一个数组,您可以按名称引用字段:

while($row = mysql_fetch_array($result))
{
    echo $row['name'] . ' says: ' . $row['message'] . '<BR>';
}

why not just do为什么不做

   while($row = mysql_fetch_array($result))
    {
            echo $row[1]." ".$row[0];
    }

Your query, auto limits it to the last 10, this will then show anything from 0 to 10 which get returned.您的查询,自动将其限制为最后 10 个,这将显示从 0 到 10 的任何返回值。

PS I added a space between username and message for readability PS为了便于阅读,我在用户名和消息之间添加了一个空格

You need $ symbols on your i variable:您的 i 变量上需要 $ 符号:

for($i=0;$i<10;$i++)
{
   echo $rows[$i][1].$rows[$i][0];
}

A more robust solution would be like this:一个更强大的解决方案是这样的:

$sql = "SELECT messages.text, users.name FROM messages INNER JOIN users ON messages.user_id=users.id ORDER BY messages.ms_id DESC LIMIT 10";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
   echo $row[1].$row[0];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM