MYSQLI/PHP 错误/Fetch assoc 循环由于在其中获取对象而仅返回一个结果

问题描述 投票:0回答:1

我正在尝试使用 php/mysqli 制作一个网站,但遇到了以下错误。

我创建了一个 Fetch_assoc 循环,并在其中放入一个“fetch_object”,以从另一个表中获取用户名。这不会给出任何错误,但它只循环一个对象,而不是来自该用户的 X 数量的视频。

        <?php
      $result = $mysqli->query("SELECT * FROM vid ORDER BY id DESC LIMIT 8");
      while($row = $result->fetch_assoc()){

        $videoId = htmlspecialchars($row['id']);
        $thumbnail = htmlspecialchars($row['thumbnail']);
        $title = htmlspecialchars($row['title']);
        $views = htmlspecialchars($row['views']);
        $likes = htmlspecialchars($row['likes']);

        $name = '';
        $result = $mysqli->query("SELECT * FROM vid_users v INNER JOIN users u ON v.user_id = u.id WHERE v.vid_id = {$videoId}");
        while($row = $result->fetch_object()){
          $name .= $row->name ." ";
        }

        echo "<li class='videoBox'>
                <!--<a class='del_video' href='?del_video={$videoId}' onclick='return confirm(\"Are you sure you want to delete this video?\");'/>x</a>-->
                <div class='videoBoxThumbnail boxShadow'>
                  <a href='/video.php?id={$videoId}' style='display: block;'>
                    <img src='{$thumbnail}' alt='Thumbnail of {$title}'>
                  </a>
                </div>
                <div class='videoBoxText'>
                  <a href='/video.php?id={$videoId}' title='{$title}'>
                    <span class=''>{$name}</span>
                    <h1>{$title}</h1>
                </div>
                    <span class='videoStatus'>
                      <p>{$views} <i class='fa-regular fa-eye'></i>   {$likes} <i class='fa-regular fa-heart'></i></p>
                    </span>
                  </a>
              </li>";
      }
    ?>
php mysqli xampp
1个回答
0
投票

您需要为两个查询使用不同的变量,以避免冲突或不正确的结果。这是适合您的固定代码。

$result = $mysqli->query("SELECT * FROM vid ORDER BY id DESC LIMIT 8");
while ($row = $result->fetch_assoc()) {

    $videoId = htmlspecialchars($row['id']);
    $thumbnail = htmlspecialchars($row['thumbnail']);
    $title = htmlspecialchars($row['title']);
    $views = htmlspecialchars($row['views']);
    $likes = htmlspecialchars($row['likes']);

    $name = '';
    // Use a different variable for the second query result
    $userResult = $mysqli->query("SELECT * FROM vid_users v INNER JOIN users u ON v.user_id = u.id WHERE v.vid_id = {$videoId}");
    while ($userRow = $userResult->fetch_object()) {
        $name .= htmlspecialchars($userRow->name) . " ";
    }

    echo "<li class='videoBox'>
            <!--<a class='del_video' href='?del_video={$videoId}' onclick='return confirm(\"Are you sure you want to delete this video?\");'/>x</a>-->
            <div class='videoBoxThumbnail boxShadow'>
              <a href='/video.php?id={$videoId}' style='display: block;'>
                <img src='{$thumbnail}' alt='Thumbnail of {$title}'>
              </a>
            </div>
            <div class='videoBoxText'>
              <a href='/video.php?id={$videoId}' title='{$title}'>
                <span class=''>{$name}</span>
                <h1>{$title}</h1>
            </div>
                <span class='videoStatus'>
                  <p>{$views} <i class='fa-regular fa-eye'></i>   {$likes} <i class='fa-regular fa-heart'></i></p>
                </span>
              </a>
          </li>";
}

变化

  1. 将第二个查询的结果重命名为
    $userResult
    以避免覆盖第一个查询中的
    $result
    变量。
  2. 在循环内使用
    htmlspecialchars
    表示
    $userRow->name
    ,以防止潜在的 XSS 攻击。
© www.soinside.com 2019 - 2024. All rights reserved.