下面的我的PHP / HTML代码显示来自网站上数据库的数据,每隔几秒刷新一次,以获得最新信息。是否可以在点击该网站的链接后五分钟终止数据库连接?为了我自己的隐私,数据库连接的字段留空。有人可以在我的代码中显示我将如何包含此内容吗?谢谢
<HTML>
<head>
<meta http-equiv="refresh" content="2">
</head>
</html>
<?php
$db = mysqli_connect('', '', '', '') or die('Error connecting to MySQL server.');
?>
<?php
$servername = "";
$username = "";
$password = "";
$database = "";
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($db, "SELECT * FROM patients2");
echo "<table border = 5>";
echo "<tr>";
echo "<th>ID</th> <th>Patient name</th>
<th>Doctor name</th>
<th>Check in date</th>
<th>Room number</th>
<th>Bed number</th>
<th>Notes</th>
<th>Time</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['patient_name'] . "</td>";
echo "<td>" . $row['doctor_name'] . "</td>";
echo "<td>" . $row['check_in_date'] . "</td>";
echo "<td>" . $row['room_number'] . "</td>";
echo "<td>" . $row['bed_number'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($db);
?>
答案 0 :(得分:0)
通过以特定间隔运行AJAX调用,您可以使用AJAX动态刷新内容2秒。这意味着页面本身不会重新加载,但您可以经常查询所需的页面。这意味着将代码分成两个文件。
首先是主文件。
<?php
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> <!-- Get latest jQuery libary -->
<script>
var refresh_timer = 2; // Seconds
var stop_timer = 60; // Number of iterations to complete
var count = 0;
$().ready(function() {
setInterval(function() {
if (count < stop_timer) {
$.ajax({
url: "load_newest.php",
success: function(data){
count++;
$("#table_body").html(data);
}
});
}
}, refresh_timer*1000);
});
</script>
</head>
<body>
<table border=5>
<thead>
<tr>
<th>ID</th>
<th>Patient name</th>
<th>Doctor name</th>
<th>Check in date</th>
<th>Room number</th>
<th>Bed number</th>
<th>Notes</th>
<th>Time</th>
</tr>
</thead>
<tbody id="table_body">
<?php
$result = $conn->query("SELECT * FROM patients2");
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['patient_name'] . "</td>";
echo "<td>" . $row['doctor_name'] . "</td>";
echo "<td>" . $row['check_in_date'] . "</td>";
echo "<td>" . $row['room_number'] . "</td>";
echo "<td>" . $row['bed_number'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "</tr>";
} ?>
</tbody>
</table>
</body>
</html>
在本例中,AJAX的目标文件名为load_newest.php
,与主文件位于同一文件文件夹中。
<?php
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = $conn->query("SELECT * FROM patients2");
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['patient_name'] . "</td>";
echo "<td>" . $row['doctor_name'] . "</td>";
echo "<td>" . $row['check_in_date'] . "</td>";
echo "<td>" . $row['room_number'] . "</td>";
echo "<td>" . $row['bed_number'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "</tr>";
}
这将使用最新查询每隔refresh_timer = 2;
秒替换表的主体,并在执行60次后停止(这是2分钟,因为您每2秒执行一次)。
答案 1 :(得分:-1)
根据这个awnser https://stackoverflow.com/a/12434864/6745860
也许你可以像这样做你的msqli_close:
Your PHP code called by the job will simply do (in pseudo code):
$batchRecords = takeAbunchOfRecordsWhereStatus(NOT_SENT);
while($batchRecords) {
if($creationDate + 10 minutes >= now()) {
sendEmail();
markRecordAsSent();
}
}