dptj13337 2016-10-20 05:19
浏览 168
已采纳

SELECT查询仅输出一个结果

I am running into an issue with a SELECT query. The query is only outputting one result, instead of all of the rows in this database.

Does anyone see why?

$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();
        }
  • 写回答

2条回答 默认 最新

  • dongzuoyue6556 2016-10-20 05:24
    关注

    You are outputting the results outside of the while loop. Since you're storing the entire result set into the $comment_array[], you could dump this as well, to get all the comments fetched from DB or, alternatively, output them inside the loop.

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了