ajax调用php脚本没有错误没有记录

时间:2017-08-16 07:47:19

标签: php mysql pdo

伙计们,请查看我的代码中的错误,这主要是由ajax发布的。相同的记录显示在其他页面中,但是我点击load more button get_data.php似乎没有从db获取记录,而iinstead iam得到一个空白页。

我已经尝试了echo $ cat,$ row,当点击加载更多时,它们会在index.php中显示记录。

<?php
require('includes/config.php'); 
$cat = 2;
$row = 2;

$rowperpage = 2;

echo $sth = $db->prepare("SELECT * from blog_posts WHERE catID = :catID ORDER BY postID ASC LIMIT :row, :rowperpage");
$sth->bindParam(':catID', $cat, PDO::PARAM_INT);
$sth->bindParam(':row', $row, PDO::PARAM_INT);
$sth->bindParam(':rowperpage', $rowperpage, PDO::PARAM_INT);
$sth->execute();

$results = $sth->fetchall(PDO::FETCH_ASSOC); 

$html = '';

 $html .= '<div class="row">';
 $html .= '<div class=" col-sm-12 col-md-6 col-lg-6">';
 $html .= '<div class="row">';
 $html .= ' <div class="leftbar_content">';

 foreach ($results as $rows) {
   $id = $rows['postID'];
     $img = $rows['postImg'];
            if(!empty($img)){
            $img ="<img src='".DIR."uploads/images/".$img."' alt=''";
            }else{
            $img = "";
            }
            $youtube = $rows['postYoutube'];
            if(!empty($youtube)){
            $youtube ='<div class="videoholder"> <iframe width="720" height="450" src="//www.youtube.com/embed/'.$youtube.'?autohide=1&fs=1&autoplay=0&iv_load_policy=3&rel=0&modestbranding=1&showinfo=0&controls=1&hd=1" frameborder="0" allowfullscreen=""></iframe></div>';
            }else{
            $youtube = "";
}                   
                $html .= '<div id="post_'.$id.'">';

            $html .= '<div class="single_stuff wow fadeInDown">';
              $html .= '<div class="single_stuff_img"> <a href="'.DIR.'post/'.$rows['postSlug'].'">'.$img.$youtube.'</a> </div>';
               $html .= '<div class="single_stuff_article">';
                $html .= '<div class="single_sarticle_inner">  <a class="stuff_category" href="'.DIR.'category/'.getCatUrl($rows['catID']).'">'.getCatName($rows['catID']).'</a>';
                  $html .= '<div class="stuff_article_inner"> <span class="stuff_date">'.date('jS M Y', strtotime($rows['postDate'])).'</span>';
                    $html .= '<h2><a href="'.DIR.'post/'.$rows['postSlug'].'">'.$rows['postTitle'].'</a></h2>';
                  $html .= '<p>'.$rows['postDesc'].'</p>';
                 $html .= '</div>';
                $html .= '</div>';
              $html .= '</div>';
            $html .= '</div>';

            $html .= '</div>';

}

echo $html;

这是我的js

<script type="text/javascript">
$(document).ready(function(){
    // Load more data
    $('.load-more').click(function(){
        var row = Number($('#row').val());
        var catid = Number($('#catid').val());
        var allcount = Number($('#all').val());
        row = row + 2;
        if(row <= allcount){
           $("#row").val(row);
            $.ajax({
                url: 'get_data.php',
                type: 'post',
                data: {row:row,catid:catid},
                beforeSend:function(){
                    $(".load-more").text("Loading...");
                },
                success: function(response){
                    // Setting little delay while displaying new content
                    setTimeout(function() {
                        // appending posts after last post with class="post"
                        $(".post:last").after(response).show().fadeIn("slow");
                        var rowno = row + 4;
                        // checking row value is greater than allcount or not
                        if(rowno > allcount){

                            // Change the text and background
                             $(".load-more").text("Sorry , No more posts to load");
                            $('.load-more').css("background","#FB0925");
                        }else{
                            $(".load-more").text("Load more");
                        }
                    }, 2000);
                }
            });
        }else{
            $('.load-more').text("Loading...");
            // Setting little delay while removing contents
            setTimeout(function() {
                // When row is greater than allcount then remove all class='post' element after 3 element
                $('.post:nth-child(3)').nextAll('.post').remove().fadeIn("slow");
                // Reset the value of row
                $("#row").val(0);
                // Change the text and background
                $('.load-more').text("Load more");
                $('.load-more').css("background","#333333");
            }, 2000);
        }
    });
});
</script> 

分享了代码,希望我能得到一些解决方案。

感谢

1 个答案:

答案 0 :(得分:0)

也许这会有所帮助。

通常在使用LIMIT子句时处于仿真模式(默认情况下处于启用状态)时,PDO会将占位符替换为实际数据,而不是单独发送。

尝试关闭仿真。

by:$db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );

然后改变这个:

echo $sth = $db->prepare("SELECT * from blog_posts WHERE catID = :catID ORDER BY postID ASC LIMIT :row, :rowperpage");

$sth = $db->prepare("SELECT * from blog_posts WHERE catID = :catID ORDER BY postID ASC LIMIT :row, :rowperpage");