【问题标题】:Refreshing data output from database (PHP)刷新数据库的数据输出 (PHP)
【发布时间】:2016-11-18 10:25:41
【问题描述】:

所以,我在下面有这段代码,它从我的数据库中获取数据,但我希望这些数据每 5 秒“刷新”一次,这样当数据库中放入符合规范的新内容时,就会显示出来。 (我不希望页面为此刷新,只刷新数据...)

$sql = "SELECT * FROM items WHERE reference = '1' ORDER BY datePosted asc LIMIT 5";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
      $title = $row["titel"];
      $description = $row["description"];
  }
} else {
    echo "Nothing found";
}

我一直在寻找 AJAX 和 PHP 的方法来做到这一点,但我似乎没有弄清楚...任何帮助/信息都非常感谢!

【问题讨论】:

  • 有点把我的垃圾扔掉了..但我尝试将它放在一个函数中,然后在 JQ 中每 5 秒调用一次函数,由于某种原因它不起作用..@ KoenHollander (Ik versta ook nl...)

标签: php ajax


【解决方案1】:

你可以使用这样的东西;

<div id='contentToUpdate'></div>


<script type="text/javascript">


    setInterval(function(){
        LoadContent(); // this will run after every 5 seconds
    }, 5000);


    function LoadContent(){

        $.ajax({

            type:"POST",
            url: "/path/to/script.php",
            data: {GenerateContent: true, PostName2: 'value2'},

            success: function(result){

                $("#contentToUpdate").html(result);

            }, error: function(result){

                alert('something went wrong');

            }, complete: function(result){

                // fires regardless of above

            }

        });

    }

</script>

在接收 Ajax 请求的 php 文件中,你可以有这样的东西;

    if(isset($_POST['GenerateContent'])){


        $aResults = array();

        $sql = "SELECT title, description FROM items WHERE reference = '1' ORDER BY datePosted asc LIMIT 5";
        $result = $conn->query($sql);

        while($row = $result->fetch()){
            $aResults[] = array('title' =>$row['title'], 'description' => $row['description']);
        }


        // if there are results output them
        if(!empty($aResults)){

            // foreach result
            foreach ($aResults as $iKey => $aResult) {
                echo "<p>";
                echo "Title: ".$aResult['title']."<br>\n";
                echo "Description: ".$aResult['description']."<br>\n";
                echo "</p>";
            }

        }else{
            echo 'No results have been loaded';
        }


        exit;
    }

【讨论】:

  • 你能把它转换成需要时间的东西吗?因为这也是我可以做的事情,所以计时的东西是我不明白的:) @atoms
  • @atoms 在我尝试这个并告诉你它不起作用之前,value 和 value2 是我在 php 中的变量的名称吗?
  • 不,它们是 javscript 变量。将其留在那里以说明如何将数据发布到 php。
  • 刚刚测试过的代码及其工作(更新)。只需更改url路径
  • 感谢您的耐心和帮助!真的让我很开心!
【解决方案2】:

使用 jQuery:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
setInterval(function()
{ 
    $.ajax({
      type:"POST",
      url:"path/to/script.php",
      success:function(data)
      {
          //do something with response data
      },
      error:function(e){
         console.log("Error Occurred");
      }
    });
}, 5000);//time in milliseconds 
</script>

【讨论】:

    【解决方案3】:

    使用 jQuery 你可以尝试这样的事情:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    
    function loadData(){
        $('#table').load('yourScript.php',function (response, status, xhr) {
             // handle the response here and set the data
        });
    }
    
    loadData(); // This will run on page load
    setInterval(function(){
        loadData(); // this will run after every 5 seconds
    }, 5000);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多