如何通过php将sql查询结果显示到php页面?

时间:2017-08-07 20:01:03

标签: php mysql ajax pdo

有人可以帮帮我吗? 我有这两个文件:

  1. keywords.php
  2. records.php
  3. keywords.php类似于用户提交keywords.php?keywords=robert

    等查询的搜索页面

    现在我想要的是将值robert发送到records.php并显示与blog_posts表中的robert名称匹配的所有记录到keywords.php页面

    my keywords.php

    <?php
    $keywords = $_GET['keywords'];
    ?>    
    <!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
            <title>Test</title>    
            <script src="bootstrap/js/jquery-1.11.1.min.js"></script>
          </head>
          <body>
    
                <div class="col-lg-12" id="results"></div>
                <div id="loader_image"><img src="loader.gif" alt="" width="24" height="24"> Loading...please wait</div>
                <div class="margin10"></div>
                <div id="loader_message"></div>
                </div>
    
         <script type="text/javascript">
              var <?php echo $keyword; ?>;
              var busy = false;
              var limit = 15
              var offset = 0;
              function displayRecords(lim, off) {
                $.ajax({
                  type: "GET",
                  async: false,
                  url: "records.php",
                  data: "limit=" + lim + "&offset=" + off,
                  cache: false,
                  beforeSend: function() {
                    $("#loader_message").html("").hide();
                    $('#loader_image').show();
                  },
                  success: function(html) {
                    $("#results").append(html);
                    $('#loader_image').hide();
                    if (html == "") {
                      $("#loader_message").html('<button class="btn btn-default" type="button">No more records.</button>').show()
                    } else {
                      $("#loader_message").html('<button class="btn btn-default" type="button">Loading please wait...</button>').show();
                    }
                    window.busy = false;
                  }
                });
              }
              $(document).ready(function() {
                // start to load the first set of data
                if (busy == false) {
                  busy = true;
                  // start to load the first set of data
                  displayRecords(limit, offset);
                 }
                $(window).scroll(function() {
                  // make sure u give the container id of the data to be loaded in.
                  if ($(window).scrollTop() + $(window).height() > $("#results").height() && !busy) {
                    busy = true;
                    offset = limit + offset;
                    // this is optional just to delay the loading of data
                    setTimeout(function() { displayRecords(limit, offset); }, 500);
                    // you can remove the above code and can use directly this function
                    // displayRecords(limit, offset);
                  }
                });
              });
            </script>
    
          </body>
        </html>
    

    这是我的records.php

    <?php
    require_once("config.php");
    
    $limit = (intval($_GET['limit']) != 0 ) ? $_GET['limit'] : 10;
    $offset = (intval($_GET['offset']) != 0 ) ? $_GET['offset'] : 0;
    
    $keywords = $_GET['keywords'];
    
    $sql = "SELECT * FROM blog_posts WHERE keyword LIKE '".$keywords."' ORDER BY postid ASC LIMIT $limit OFFSET $offset";
    try {
      $stmt = $DB->prepare($sql);
      $stmt->execute();
      $results = $stmt->fetchAll();
    } catch (Exception $ex) {
      echo $ex->getMessage();
    }
    if (count($results) > 0) {
      foreach ($results as $res) {
        echo '<h3>' . $res['keyword'] . '</h3>';
      }
    }
    ?>
    

    非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

在keywords.php中需要进行一些更改。看看下面的变化。

<?php
$keywords = $_GET['keywords'];
?>      
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Test</title>     
    <script src="bootstrap/js/jquery-1.11.1.min.js"></script>
</head>
<body>
    <div class="col-lg-12" id="results"></div>
    <div id="loader_image"><img src="loader.gif" alt="" width="24" height="24"> Loading...please wait</div>
    <div class="margin10"></div>
    <div id="loader_message"></div>
    </div>

<script type="text/javascript">
    var keywords = '<?php echo $keywords; ?>';  // Changed
    var busy = false;
    var limit = 15
    var offset = 0;
    // Changed/New
    var data = {
        'limit': limit,
        'offset': offset,
        'keywords': keywords,
    };
    function displayRecords(lim, off) {
        $.ajax({
        type: "GET",
        async: false,
        url: "records.php",
        data: data, // Changed
        cache: false,
        beforeSend: function() {
            $("#loader_message").html("").hide();
            $('#loader_image').show();
        },
        success: function(html,textStatus,jqHXR) {
            $("#results").append(html);
            $('#loader_image').hide();
            if (html == "") {
                $("#loader_message").html('<button class="btn btn-default" type="button">No more records.</button>').show()
            } else {
                $("#loader_message").css('display','none');
                $("#loader_message").html(html);
            }
            window.busy = false;
        },
        });
    }
    $(document).ready(function() {
        // start to load the first set of data
        if (busy == false) {
            busy = true;
            // start to load the first set of data
            displayRecords(limit, offset);
         }
        $(window).scroll(function() {
            // make sure u give the container id of the data to be loaded in.
            if ($(window).scrollTop() + $(window).height() > $("#results").height() && !busy) {
                busy = true;
                offset = limit + offset;
                // this is optional just to delay the loading of data
                setTimeout(function() { displayRecords(limit, offset); }, 500);
                // you can remove the above code and can use directly this function
                // displayRecords(limit, offset);
            }
        });
    });
</script>

</body>
</html>