【问题标题】:SELECT query only outputting one resultSELECT 查询只输出一个结果
【发布时间】:2017-03-01 22:57:39
【问题描述】:

我遇到了SELECT 查询的问题。该查询仅输出一个结果,而不是该数据库中的所有行。

有人知道为什么吗?

$select_comments_sql = "
    SELECT * 
    FROM home_comments
    ORDER BY id DESC
";
  if ($select_comments_stmt = $con->prepare($select_comments_sql)) {
        //$select_comments_stmt->bind_param("s", $user_id);
        $select_comments_stmt->execute();
        if (!$select_comments_stmt->errno) {
            //echo "error";
        }
        $select_comments_stmt->bind_result($comment_id, $comment_user_id, $comment_username, $home_comments, $comment_date);

        $comment_array = array();
        while ($select_comments_stmt->fetch()) {
            $comment_array[] = $comment_user_id;
            $comment_array[] = $comment_username;
            $comment_array[] = $home_comments;
            $comment_array[] = $comment_date;
        }
        if ($home_comments === NULL) {
            echo 'No comments found.';
        } else {
            echo $comment_username. "<br>";
            echo $home_comments. "<br><br><br>";
        }
  }

AJAX

$("#comment-form").on("submit", function (event) {
    event.preventDefault();

    var home_comment = $("#home_comment").val();

    $.ajax({ 
        url: "ajax-php/comment-send.php", 
        type: "POST",
        data: {
            "home_comment": home_comment
        },
        success: function (data) {
        //  console.log(data); // data object will return the response when status code is 200
            if (data == "Error!") {
                alert("Unable to post comment!");
                alert(data);
            } else {
                $("#comment-form")[0].reset();
                //$('.newsletter-popup').fadeIn(350).delay(2000).fadeOut();
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(textStatus + " | " + errorThrown);
            console.log("error"); //otherwise error if status code is other than 200.
        }
    });
});

PHP

$user = new User();

    $home_comment = $_POST['home_comment'];
    $username = $user->data()->username;
    $okay = true;

    if ( $okay ) { 

        $comment_insert = "
            INSERT INTO home_comments 
            (id, user_id, username, comment, date)
            VALUES(?, ?, ?, ?, NOW())
            ";
        $comment_stmt = $con->prepare($comment_insert);
        $comment_stmt->bind_param('ssss', $id, $user_id, $username, $home_comment);
        $comment_stmt->execute();
        }

【问题讨论】:

  • 请检查您的while循环和对数组的赋值是否正确。似乎它正在替换值,只有最后一个值应该在那里
  • 由于您在循环外回显结果,因此您只会得到最后一个结果。

标签: php mysql pdo prepared-statement


【解决方案1】:

您在 while 循环之外输出结果。由于您将整个结果集存储到 $comment_array[] 中,因此您也可以转储它,以便从数据库中获取所有 cmets,或者在循环中输出它们。

if ($select_comments_stmt = $con->prepare($select_comments_sql)) {
    //$select_comments_stmt->bind_param("s", $user_id);
    $select_comments_stmt->execute();
    if (!$select_comments_stmt->errno) {
        //echo "error";
    }
    $select_comments_stmt->bind_result($comment_id, $comment_user_id, $comment_username, $home_comments, $comment_date);

    $comment_array = array();
    while ($select_comments_stmt->fetch()) {
        $comment_array[] = $comment_user_id;
        $comment_array[] = $comment_username;
        $comment_array[] = $home_comments;
        $comment_array[] = $comment_date;
        if ($home_comments === NULL) {
            echo 'No comments found.';
        } else {
            echo $comment_username. "<br>";
            echo $home_comments. "<br><br><br>";
        }
    }
    // Alternatively: print_r($comment_array);
}

【讨论】:

  • 谢谢!快速提问。此选择查询用于页面加载以显示 cmets。但是,我有一个 ajax 调用,它在提交时插入 cmets,我可以在 ajax 中添加这个相同的选择查询,在插入下使其在提交后立即显示新评论(没有页面加载)还是我必须做一些事情还有吗?
  • 运行整个查询并再次获取整个结果集并不是一个好主意。您可以编写 ajax 请求来仅获取新的 cmets。
  • @Paul 推迟内容加载是一种很好的做法。您还应该对调用中获取的 cmets 数量设置限制。我也建议与上述 Suhail 评论的相同。
  • 如何只获取新的 cmets?我添加了用于插入 cmets 的 ajax 和 php 文件,我希望可以将其添加到这些文件中,而不是创建两个新文件。
  • @Paul 有几个相同的问题:stackoverflow.com/q/25819790stackoverflow.com/q/11229645
【解决方案2】:

您编写的代码存在几个问题。首先,您将每一列的值附加到 $comment_array 数组中。您应该在 while 循环中创建一个子数组,并在 $comment_array 中创建一个子数组来制作多维数组。

其次,您只是显示 $comment_username 的值,这将是 while 循环中的最后一个用户名。

您应该遍历 $comment_array 并分别显示每个用户名和 cmets。

代码看起来像这样,

    $comment_array = array();
    while ($select_comments_stmt->fetch()) {
        $sub_array=array();
        $sub_array["userid"] = $comment_user_id;
        $sub_array["username"] = $comment_username;
        $sub_array["comments"] = $home_comments;
        $sub_array["date"] = $comment_date;
        $comment_array[]=$sub_array;
    }
    if ($home_comments === NULL) {
        echo 'No comments found.';
    } else {
        echo $comment_username. "<br>";
        echo $home_comments. "<br><br><br>";

        // Loop over comments array.

        foreach($comment_array as $comment) {
            echo $comment["username"]. "<br>";
            echo $comment["comments"]. "<br><br><br>";
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 2016-09-14
    相关资源
    最近更新 更多