【问题标题】:PHP while command missing first entryPHP while 命令缺少第一个条目
【发布时间】:2018-05-21 22:24:16
【问题描述】:

我正在尝试使用 while 循环从 MySQL 数据库中打印多个条目。在我这样做的任何地方,它都缺少列表中的第一项,并且只能从第二项开始打印。

<?php
require 'db.php';
$udate = $mysqli->escape_string($_POST['date']);
$result = $mysqli->query("SELECT * FROM videos WHERE date='$udate'");
$user = $result->fetch_assoc();
$_SESSION['file_name'] = $user['file_name'];
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>View</title>
        <link rel="stylesheet" type="text/css" href="css/nominatorstyle.css">  
    </head>
    <body>
        <div class="header">
            <h2>Results.</h2>
            <form action="watch.php" method='POST'><table>
                <?php 
                    if($result->num_rows > 0){
                        while($row = $result->fetch_assoc()){
                            $vid = $row['vid'];                           
                            $source = "uploads/".$row['file_name'];
                            echo "<tr><td>".$row['vid']."</td>
<td>".$row['file_name']."</td>";
                            echo "<td><a href=watch.php?
source=$source>Play</a></td></tr>";
                        }
                    }else{
                    echo "0 results";
                    } 
                ?>
                </table></form>
            <a href="profile.php"><button class="button-block">Home</button></a>
        </div>      
    </body>

我希望它打印所有匹配的条件。 我只收到第一个之后的条目。

【问题讨论】:

  • 所以您的查询有问题。这段代码看起来不错。
  • @AdamKozlowski:呃……为什么?在哪里?
  • @panther 这里是查询$result = $mysqli-&gt;query("SELECT * FROM videos WHERE date='$udate'");
  • 查询和while循环之间有代码吗?
  • 也许您想更改您的查询? bobby-tables.com

标签: php mysql while-loop phpmyadmin


【解决方案1】:

您在 while 循环结束时错过了一个“}”,这可能是个问题。我还将您的 >=1 的 num_rows 条件更改为 > 0。试试这个:

if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $vid = $row['vid'];                           
        $source = "uploads/" . $row['file_name'];
        $htmlRow  = "<tr>\n";
        $htmlRow .= "<td>" . $row['vid'] . "</td>\n";
        $htmlRow .= "<td>" . $row['file_name'] . "</td>\n";
        $htmlRow .= "<td>" . $row['vid'] . "</td>\n";
        $htmlRow .= "<td><a href=\"watch.php?source=" . $source . "\">Play</a></td>\n";
        $htmlRow .= "</tr>\n";
        echo $htmlRow;
    }
}

【讨论】:

  • 我试过这个,它导致&lt; r&gt; 打印在列表的顶部,但仍然缺少第一个条目。最后一个&lt;\tr&gt; 应该是&lt;/tr&gt; 仍然只是第二个条目。
  • 你是对的 打错了。在评论中编辑。你给了-1 ..你能直接从你的mysql数据库显示你的查询输出吗?这应该有效。
  • 刚刚看了你更新的代码,问题是因为你使用了两次"$result->fetch_assoc()"。
  • 我没有给你负面反馈。另外,我还没有它的声誉。
【解决方案2】:
        require 'db.php';
        $udate = $mysqli->escape_string($_POST['date']);
        $result = $mysqli->query("SELECT * FROM videos WHERE date='$udate'");

        /*
         This fetch_assoc will get your first row #0 of the query
         next fetch_assoc with your loop on the $result 
         will start as you said from row #1
        */
        $user = $result->fetch_assoc(); //your missing row #0
        $_SESSION['file_name'] = $user['file_name'];

【讨论】:

  • 就是这样。谢谢!
猜你喜欢
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 2022-11-05
相关资源
最近更新 更多