gpt4 book ai didi

php - 从mysql打印数据

转载 作者:行者123 更新时间:2023-11-29 10:08:34 25 4
gpt4 key购买 nike

在类似信息亭的应用程序中,我尝试以设定的间隔(我的情况是 60 秒)打印 mysql 数据库中插入的最后 60 秒记录。

使用下面的代码是否有可能因 ajax 刷新延迟或任何省略打印记录的情况?如果是,我可以采取什么措施来避免这种情况?

   <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
@media print {
tr.page-break {
display: block;
page-break-before: always;
}
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
function getData(){
$.ajax({
type: 'GET',
url: 'data.php',
success: function(data){
$('#output').html(data);

function isEmpty( el ){
return !$.trim(el.html())
}
if (!isEmpty($('#output'))) {
window.print();
}
}
});
}

getData();
setInterval(function () { getData(); }, 60000); // it will refresh your data every 1 sec

});
</script>
</head>

<body>
<div class="container" id="output"></div>
</body>
</html>

还有data.php:

<?php

$servername = "localhost";
$username = "root";
$password = "xxx";
$dbname = "testdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "select * from orders where time > date_sub(now(), interval 1 minute) ORDER BY id DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table>";
// output data of each row
while($row = $result->fetch_assoc()) {
$data1 =$row["id"];
$data2= $row["product"];
$data3= $row["details"];
echo "<tr class='page-break'>";
echo "<td>" . $data1 . "</<td>";
echo "<td>" . $data2 . "</td>";
echo "<td>" . $data3 . "</td>";
echo "</tr>";
}
echo "</table>";
}
$conn->close();

?>

该代码仅用于测试而非生产,因此不要介意 php 安全问题(sql 注入(inject)等)。但如果您能以任何方式改进 javascript 部分,我将不胜感激。

还有比这更好的解决方案来打印 mysql 数据库中最后 60 秒的记录吗?

最佳答案

根据@Adder的代码,我最终得到了这个:

<script>
$(document).ready(function () {

$.ajax({ // Get lastID
type: 'GET',
url: 'lastid.php',
dataType: 'json',
'success': function (data) {
callback(data);
}
});
var return_last;
function callback(response) {
return_last = response;
var lastID = return_last.id;




function getData() {
$.ajax({
type: 'GET',
url: 'data.php',
data: {lastID: lastID},
dataType: 'json',
success: function (data) {
lastID = data[0].id;
console.log(lastID);
$.each(data, function (i, item) {
var $tr = $('<tr class="page-break">').append(
$('<td>').text(item.id),
$('<td>').text(item.name),
$('<td>').text(item.details)
).appendTo('#output');
});

function isEmpty(el) {
return !$.trim(el.html());
}
if (!isEmpty($('#output'))) {
window.print();
}

}
});
}

getData();


setInterval(function () {
$("tr").addClass("no-print");
getData();
}, 60000); // it will refresh your data every 1 minute
}
});
</script>

和sql

$lastID = $_GET['lastID'];
$sql = "select * from orders where id > ".$lastID." ORDER BY id DESC";
$result = $conn->query($sql);
$dataArray = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$dataArray[] = $row;
}

echo json_encode($dataArray);

}

关于php - 从mysql打印数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51420776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com