我几天来一直在寻找解决这个问题的方法,最后还是决定问你们可爱的人,因为似乎有多种方法可以让这只谚语变成猫。
我有一个php页面(index.php),当前使用以下外部php脚本(script.php)显示SQL select查询的结果:
<?php
{
include('database.php');
// Microsoft SQL Server using the SQL Native Client 11.0 ODBC Driver
$conn = odbc_connect($server,$Username,$Password);
// Query
$qry = $Query; <-- this var is set in the included database.php above
$nextbreach = odbc_exec($conn,$qry);
odbc_close();
}
$nextbreachvalue = odbc_result($nextbreach,"Breached Time");
$nextbreachref = odbc_result($nextbreach,"Reference");
?>
然后使用在我的index.php中回显 $nextbreachvalue
和$nextbreachref
<?php echo ($nextbreachref); ?>
<?php echo ($nextbreachvalue); ?>
我可以这样做,因为我使用了{include('script.php');}
虽然这适用于在浏览器中加载index.php,但我希望每秒更新变量$nextbreachref
和$nextbreachvalue
,而无需刷新页面。
我见过许多文章建议ajax,json和js可以实现这一点但是我对我在这种情况下如何实现这一点感到困惑,特别是因为很多在线示例都使用mysql而不是mssql。
Index.php 如下:
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="assets/css/style.css">
<?php
{include('script.php');}
?>
</head>
<body>
<div id="container">
<div id="countdowncontainer">
<div id="countdownclock">
<p id="ref" ><?php echo ($nextbreachref); ?></p>
</div>
</div>
</div>
答案 0 :(得分:0)
您尝试做的基本结构是:
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="assets/css/style.css">
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script>
$(function() {
setInterval(function(){
$.get("data.php", function( data ) { //receiving the echo from the php data file
$("#ref").html(data); //putting the echoed value directly into the p tag
});
}, 1000); //setting the requests to 1 second each
);
</script>
</head>
<body>
<div id="container">
<div id="countdowncontainer">
<div id="countdownclock">
<p id="ref" ></p>
</div>
</div>
</div>
您的data.php
类似于您从数据库中获取数据的代码。不同之处在于它现在回显了您想要在主页面中检索的结果,并且不包含任何html:
<?php
{
include('database.php');
// Microsoft SQL Server using the SQL Native Client 11.0 ODBC Driver
$conn = odbc_connect($server,$Username,$Password);
// Query
$qry = $Query; <-- this var is set in the included database.php above
$nextbreach = odbc_exec($conn,$qry);
odbc_close();
}
$nextbreachvalue = odbc_result($nextbreach,"Breached Time");
$nextbreachref = odbc_result($nextbreach,"Reference");
echo($nextbreachref);
?>