Johnny Catsville
Johnny Catsville

Reputation: 55

PHP, Ajax Infinite Scroll converting code from Mysqli to PDO

I'm trying to convert Mysqli code to PDO for Ajax infinite Scroll system I have found on internet, so I can add it to the blog I'm making for learning php. This is the original ajax.php file:

 if( isset( $_POST['start'] ) && isset( $_POST['limit'] ) && !empty( $_POST['start'] ) && !empty( $_POST['limit'] ) ){
    $start = $_POST['start'];
    $limit = $_POST['limit'];
    $query = "SELECT * FROM timeline limit $start, $limit";
    $result = mysqli_query($con, $query) or die('Error: ' . mysqli_error($con));
    $data = array();
    $rowcount = mysqli_num_rows($result);
    $data['count'] = $rowcount;
    while($row = mysqli_fetch_assoc($result)) {
        $data['content'][] = $row;
    }
    mysqli_close($con);
    echo json_encode($data);exit;   
}

Here is my try, everything looks fine to me, but still it's not working, so I guess I messed up with some PDO functions...

    if( isset( $_POST['start'] ) && isset( $_POST['limit'] ) && !empty( $_POST['start'] ) && !empty( $_POST['limit'] ) ){
    $start = $_POST['start'];
    $limit = $_POST['limit'];
    $stmt = "SELECT * FROM timeline limit $start, $limit";
    $data = array();
    $rowcount = $stmt->fetchAll(); 
    $data['count'] = $rowcount;
    foreach ($db->query($stmt) as $row) {
    $data['content'][] = $row;
}
echo json_encode($data);
}

I know I should use PDO prepared statements and I will, but first I wanna make it works as it is...

And here is Ajax script.js itself:

flag = true;
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()){
first = $('#first').val();
limit = $('#limit').val();
no_data = true;
if(flag && no_data){
    flag = false;
    $('#loader').show();
    $.ajax({
        url : 'ajax.php',
        dataType: "json",
        method: 'post',
        data: {
           start : first,
           limit : limit
        },
        success: function( data ) {
            flag = true;
            $('#loader').hide();
            if(data.count > 0 ){
                first = parseInt($('#first').val());
                limit = parseInt($('#limit').val());
                $('#first').val( first+limit );
                $('#timeline-container');
                $.each(data.content, function(key, value ){

                    if(value.event!=''){
                    html = '<li class="timeline-item">';
                    html += '<div class="timeline-badge" data-
                    toggle="popover" data-placement="left" data-
                    trigger="hover" data-
                    content="Mention"><a href="#"></a></div>';
                    html += '<div class="timeline-panel">';
                    html += '<div class="timeline-heading">';
                    html += '<p>This is the new post: </p>';
                    html += '<div class="timeline-date"><i class="fa fa-
                    calendar-o"></i> '+value.date+'</div>';
                    html += '</div>';
                    html += '<div class="timeline-content">';
                    html += '<p>'+value.post+'</p>';
                    html += '</div>';
                    html += '</li>';
                    }

                    $('#timeline-container').append( html );

                    $('.timeline-item').waypoint({
                        triggerOnce: true,
                        offset: '80%',
                        handler: function() {
                            jQuery(this).addClass('animated fadeInUp');
                        }
                    });
                });
            }else{
                alert('No more data to show');
                no_data = false;
            }
        },
        error: function( data ){
            flag = true;
            $('#loader').hide();
            no_data = false;
            alert('Something went wrong, Please contact admin');
        }
    });
}

} });

Here is my config file in case it's needed... thanks everyone in advance!

define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','');
define('DBNAME','my_db');

$db = new PDO("mysql:host=".DBHOST.";charset=utf8;dbname=".DBNAME, DBUSER, 
DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

define('FIRST', 4);
define('LIMIT', 4);

Upvotes: 0

Views: 366

Answers (1)

Barmar
Barmar

Reputation: 780724

This line is totally wrong:

$rowcount = $stmt->fetchAll(); 

$stmt is a string, not a PDO object, you can't call fetchAll() on it. And fetchAll() returns all the rows of results, not the count.

if( isset( $_POST['start'] ) && isset( $_POST['limit'] ) && !empty( $_POST['start'] ) && !empty( $_POST['limit'] ) ){
    $start = $_POST['start'];
    $limit = $_POST['limit'];
    $data = array();
    $stmt = $db->prepare("SELECT * FROM timeline limit :start, :limit";
    $stmt->execute(array(':start' => $start, ':limit' => $limit));
    $data['content'] = $stmt->fetchAll(); 
    $data['count'] = count($data['content']);
}
echo json_encode($data);

Upvotes: 1

Related Questions