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

标签 php mysql pdo

伙计们,请看看我的代码有什么问题,基本上是通过 ajax post 调用的。相同的记录显示在其他页面中,但我单击 load more button get_data.php 似乎没有从数据库中获取记录,而是我得到一个空白页面。

我试过 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;

这是我的代码

<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> 

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

谢谢

最佳答案

也许这会有所帮助。

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

尝试关闭仿真。

通过:$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");

关于php - ajax调用php脚本没有错误没有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45707896/

相关文章:

php - 我应该从 mysql 升级到 PDO 的 mysqli 吗?

php - 使用位于另一台服务器上的 PHP 文件

php - SQLSTATE [HY000] [1045]用户拒绝访问-PHP PDO方法

php - 使用先前在其他类中声明的对象

php - PDO 查询返回空

php - 如何以编程方式对查看结果进行排序?

php - 如何使用 `preg_replace()` 隐藏部分 IP 地址和/或主机名?

php - 如何使用 psudo 列进行 ORDER BY?

php - 从 SQL 数据库中选择数据并在表中显示不起作用

php - 用于插入 MySQL 数据库的 python 脚本 - 特殊符号 - unicode : search and store