【问题标题】:ajax call php script no errors not recordsajax调用php脚本没有错误没有记录
【发布时间】:2018-01-24 06:17:57
【问题描述】:

请大家看看我的代码有什么问题,基本上是由 ajax 帖子调用的。相同的记录显示在其他页面中,但我单击 load more button get_data.php 似乎没有从 db 获取记录,而是我得到一个空白页面。

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

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

谢谢

【问题讨论】:

  • 请发布您的javascript函数。
  • @TrươngCôngHậu 我刚刚用 js 更新了它
  • 1- $results = $sth->fetchall(PDO::FETCH_ASSOC);在 ajax 函数中打印此结果 2-alter 响应可能是您遇到的问题。
  • 我做了这个 echo print_r($results);并得到 Array ( ) 1
  • 用这个查询数据显示 $results = $db->query('SELECT * FROM blog_posts WHERE catID = 2 ORDER BY postID ASC LIMIT 1,2')->fetchAll();

标签: php mysql pdo


【解决方案1】:

也许这会有所帮助。

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

尝试关闭仿真。

作者:$db-&gt;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");

【讨论】:

  • 收到此错误 致命错误:未捕获异常 'PDOException' 并带有消息 'SQLSTATE[HY093]:无效参数编号:未定义参数'
  • 像这样使用它,我得到的记录显示 $results = $db->query('SELECT * FROM blog_posts WHERE catID = '.$cat.' ORDER BY postID DESC LIMIT '.$row.' ,'.$records_per_page.'')->fetchAll();
  • 但我需要 BindParam 中的参数,但如果我绑定它们就不起作用
  • 尝试执行(array(YOUR PARAMS))
  • $sth = $db->prepare("SELECT * from blog_posts WHERE catID = ? ORDER BY postID ASC LIMIT ?,?"); $sth->execute(array($cat,$row,$rowperpage)); $sth->执行(); $rows = $sth->fetchall(PDO::FETCH_ASSOC);
猜你喜欢
  • 2013-05-08
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多