1

I have a table consists of three columns :

id
author
message

Once the user clicks on the More Button the button passes the Count parameter to .load function. And then new data should display but it displays a blank page.What am i doing wrong?

this is index.php:

<div id="comments">
        <?php 
        $v=$db->prepare('select * from  comments limit 2');
        $v->execute();
        $data=$v->fetchAll(PDO::FETCH_ASSOC);
        foreach ($data as $row) {
            echo "<div>".$row['author']."</div>";
            echo "<div>".$row['message']."</div>";
            echo "</br>";
        }
        ?>
</div>
    <input type="submit" name="" id="submit" value="More">
</body>
</html>

<script type="text/javascript">
    $(document).ready(function(){
        var commentCount=2;
        $('#submit').click(function(){
            commentCount=commentCount+2;
            $('#comments').load('loadComments.php',{count:commentCount});
        });
    });
</script>

this is loadComments.php:

<?php 
$db=new PDO('mysql:host=localhost;dbname=ajax',"root","");
$newCount=$_POST['count'];

$ex=$db->prepare('select * from comments limit $newCount');
$ex->execute();
$data=$ex->fetchALL(PDO::FETCH_ASSOC);

foreach ($data as $row) {
    echo "<div>".$row['author']."</div>";
    echo "<div>".$row['message']."</div>";
    echo "</br>";
}

?>

EDİT:

If I use this way everything is ok.

$v=$db->prepare('select * from  comments limit 3');

But I have checked count parametre inside loadComment.php

echo $newCount;

and I am able to get the value Its funny

dWinder
  • 11,597
  • 3
  • 24
  • 39

1 Answers1

2

The issue is because of using Single quoted strings (as ') instead of Double quote strings (as ") - as @mrunion mention in the comments

In PHP when using ' the inner string is not being evaluate in contrast to the " mark. So the statement of 'select * from comments limit $newCount' is been sent as is and the $newCount is not been evaluate as 2 or what ever int it hold.

Full explanation about PHP quote can be found here

dWinder
  • 11,597
  • 3
  • 24
  • 39