【问题标题】:My correct MySQL query that is supposed to return 4 images, returns only 3 [duplicate]我正确的 MySQL 查询应该返回 4 个图像,只返回 3 个 [重复]
【发布时间】:2017-09-16 15:08:12
【问题描述】:

即使当我在 phpMyAdmin SQL 选项卡中查询它时它可以正常工作。我想要完成的是显示用户上传的所有图像(目前是 4 个),但我只得到 3 个(缺少的总是第一个)。这是我的代码:

<?php

    $currentUser = $_SESSION['id'];
    $sql = "SELECT username FROM user WHERE id='$currentUser'";
    $result = mysqli_query($conn, $sql);
    $getResult = mysqli_fetch_assoc($result);
    $author = $getResult['username'];

    $sql2 = "SELECT * FROM image WHERE author='$author' ORDER BY id DESC";
    $result2 = mysqli_query($conn, $sql2);
    $getResult2 = mysqli_fetch_assoc($result2);

    while ($row = $result2->fetch_assoc()){
        echo '<a href="imageInfo.php?image='.$row["path"].'"><img class="profilePageImages" src="uploads/'.$row['path'].'" alt="Random image" /></a>';
    }

?>

【问题讨论】:

  • 删除此声明,$getResult2 = mysqli_fetch_assoc($result2);。有了这个,您已经从结果集中获取了一行,所以while() 循环将从第二行开始。
  • 你试过转储这个脚本的一部分吗?
  • 参数化你的查询,这对注入开放。您也可以通过 1 个查询来执行此操作,请查看 joins。

标签: php mysql


【解决方案1】:

这一行,在循环消耗第一行之前。删掉就好了。

$getResult2 = mysqli_fetch_assoc($result2);

【讨论】:

    猜你喜欢
    • 2013-06-19
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2021-02-24
    • 2012-12-08
    相关资源
    最近更新 更多