'for'循环数据库没有显示结果

时间:2022-09-10 19:21:53

Essentially, I built a small text chat type application for me and my friends and now the messages seem to be getting kind of long on the page so I thought it be better if I could just display the latest 10 or 20 messages or so.

基本上,我为我和我的朋友构建了一个小型文本聊天类型的应用程序,现在消息似乎在页面上变得很长,所以我认为如果我只能显示最新的10或20条消息会更好。

That said, I tried a for loop like so:

也就是说,我试过这样的for循环:

$row = mysqli_num_rows($dbqDoIt4_mssgs);
for($i = 0; $i<5; $i++){
    if($row[$i]<5){
        echo '<div class="holdChat"><span class="orangeBigger">' . strtoupper($row['user']). '</span>' . ' ' . '<span class="txtSaid">said:</span> ' . '<span style="color:#171717;">' . $row['mssg'] . '</span>' . '</div><br/>';
    }
}

This last part of the code is kind of long, but essentially it holds the CSS classes, etc. for a gray box with the database text in it.

代码的最后一部分有点长,但基本上它包含CSS类等,用于包含数据库文本的灰色框。

It now does display five rows (or however many I tell it to). I get no errors for the code or PHP, but the message itself from the database does not show, so all I get are empty gray boxes on the HTML where the text is supposed to be.

它现在显示五行(或者我告诉它多少行)。我没有得到代码或PHP的错误,但是数据库中的消息本身没有显示,所以我得到的是HTML应该是文本的空灰色框。

I'm assuming, because maybe I'm referencing the $row as $row[i], that it's not directly picking up the message(?)

我假设,因为我可能将$ row引用为$ row [i],它不是直接接收消息(?)

(I am building a small application to teach myself PHP.)

(我正在构建一个小应用程序来自学PHP。)

2 个解决方案

#1


1  

I would do something like this:

我会做这样的事情:

<?php
    $i = 0;
    while($row = mysql_fetch_array($query))
    {        
        $i++;
        echo '<div class="holdChat"><span class="orangeBigger">' . strtoupper($row['user']). '</span>' . ' ' . '<span class="txtSaid">said:</span> ' . '<span style="color:#171717;">' . $row['mssg'] . '</span>' . '</div><br/>';
        if ($i == 5)
        {
            break;
        }
    }
?>

#2


2  

From the PHP documentation for mysqli_num_rows:

从mysqli_num_rows的PHP文档:

mysqli_result::num_rows -- mysqli_num_rows — Gets the number of rows in a result

mysqli_result :: num_rows - mysqli_num_rows - 获取结果中的行数

Your $row variable is being assigned a numeric value, which doesn't mean much when you loop through it.

你的$ row变量被分配了一个数值,当你循环它时,这并不意味着什么。

I haven't worked with MySQLi, but with MySQL, you do this:

我没有使用MySQLi,但是使用MySQL,你可以这样做:

while ($row = mysql_query($query_string)) {
  ...
}

And while $row is being set (every loop is a new row), your code runs. Just add a counter to make sure you don't print more than five times and your code should work fine.

虽然正在设置$ row(每个循环都是一个新行),但代码会运行。只需添加一个计数器,以确保您不会打印超过五次,您的代码应该可以正常工作。

#1


1  

I would do something like this:

我会做这样的事情:

<?php
    $i = 0;
    while($row = mysql_fetch_array($query))
    {        
        $i++;
        echo '<div class="holdChat"><span class="orangeBigger">' . strtoupper($row['user']). '</span>' . ' ' . '<span class="txtSaid">said:</span> ' . '<span style="color:#171717;">' . $row['mssg'] . '</span>' . '</div><br/>';
        if ($i == 5)
        {
            break;
        }
    }
?>

#2


2  

From the PHP documentation for mysqli_num_rows:

从mysqli_num_rows的PHP文档:

mysqli_result::num_rows -- mysqli_num_rows — Gets the number of rows in a result

mysqli_result :: num_rows - mysqli_num_rows - 获取结果中的行数

Your $row variable is being assigned a numeric value, which doesn't mean much when you loop through it.

你的$ row变量被分配了一个数值,当你循环它时,这并不意味着什么。

I haven't worked with MySQLi, but with MySQL, you do this:

我没有使用MySQLi,但是使用MySQL,你可以这样做:

while ($row = mysql_query($query_string)) {
  ...
}

And while $row is being set (every loop is a new row), your code runs. Just add a counter to make sure you don't print more than five times and your code should work fine.

虽然正在设置$ row(每个循环都是一个新行),但代码会运行。只需添加一个计数器,以确保您不会打印超过五次,您的代码应该可以正常工作。