Get data from database without refreshing browser using ajax and php

 
 
index.php
 
<?php 
 include('dbh.php');
?>
<html>
   <head>
<script
         integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
         crossorigin="anonymous">
</script>
<script>
    $(document).ready(function(){
  var commentCount = 2;
$("button").click(function(){
    commentCount = commentCount + 2; 
$("#comments").load("load-comments.php",{
                       commentNewCount:commentCount
 
     });
});
});
     </script>
    </head>
    <body>
<div id="comments">
   <?php
    
     $sql = "select * from comments  LIMIT 2";
 
$result = mysql_query($sql,$con);
 
if(mysql_num_rows($result) > 0){
   while($row=mysql_fetch_assoc($result)){
     echo "<p>";
echo $row['author'];
echo "<br>";
echo $row['message'];
echo "</p>";
   }
}
else{
     echo "There are no comments!";
}
   ?>
 
</div>
<button>Show More Comments</button>
 
    </body>
</html>
 
Creating database connection
 
dbh.php
 
<?php 
error_reporting(E_ALL ^ E_DEPRECATED);
$con=mysql_connect("localhost","root","");
mysql_select_db('ajax',$con);
 
?>
 
load-comments.php
 
<?php
 
 $commentNewCount = $_POST['commentNewCount']; 
  include('dbh.php');
 $sql   = "select * from comments  LIMIT $commentNewCount"; 
$result = mysql_query($sql,$con);
 
if(mysql_num_rows($result) > 0){
   while($row = mysql_fetch_assoc($result)){
     echo "<p>";
echo $row['author'];
echo "<br>";
echo $row['message'];
echo "</p>";
   }
}
else{
     echo "There are no comments!";
}
?>