我正忙着为我们的一个办公室仪表板编写一个计数计时器,该计时器计算自上次事件/重置以来的时间。
我有一个页面,其中一位高级管理员可以输入新的时间戳,然后使用时间戳更新数据库中的表,我的下一页(我遇到问题的页面)连接到数据库,收集数据(通过 php)将其存储在变量中。然后,我通过 json 导出变量,并将其作为计数脚本中的事件日期。
但是,当我运行此命令时,它会为输入的每个日期显示错误的计数标准 16534 天。
下面是我的代码。你能告诉我我做错了什么吗:
<html>
<head>
<style>
#countup p {
display: inline-block;
padding: 5px;
background: transparent;
margin: 0 0 20px;
font-family: sans-serif;
font-size: 57px;
}
</style>
<title>Days Without Incident</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<?php
$servername = "10.32.6.151";
$username = "server";
$password = "";
$dbname = "incident-counter";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection Failed : ". $conn->connect_error);
}
$sql = "SELECT * FROM counter";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
}
} else {
echo "0 results";
}
?>
<script>
var counter1 = <?php echo json_encode($row[date]) ?>
window.onload=function() {
// Month,Day,Year,Hour,Minute,Second
upTime(counter1); // ****** Change this line!
}
function upTime(countTo) {
now = new Date();
countTo = new Date(countTo);
difference = (now-countTo);
days=Math.floor(difference/(60*60*1000*24)*1);
hours=Math.floor((difference%(60*60*1000*24))/(60*60*1000)*1);
mins=Math.floor(((difference%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
secs=Math.floor((((difference%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
document.getElementById('days').firstChild.nodeValue = days;
document.getElementById('hours').firstChild.nodeValue = hours;
document.getElementById('minutes').firstChild.nodeValue = mins;
document.getElementById('seconds').firstChild.nodeValue = secs;
clearTimeout(upTime.to);
upTime.to=setTimeout(function(){ upTime(countTo); },1000);
}
</script>
<!-- Save for Web Slices (Untitled-1) -->
<table id="Table_01" width="1920" height="1080" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="3">
<img src="images/incident_01.png" width="1920" height="588" alt=""></td>
</tr>
<tr>
<td rowspan="2">
<img src="images/incident_02.png" width="614" height="492" alt=""></td>
<td background="images/incident_03.png"><div id="countup" align="center">
<p id="days">00</p>
<p class="timeRefDays">days</p><br />
<p id="hours">00</p>
<p class="timeRefHours">hours</p><br />
<p id="minutes">00</p>
<p class="timeRefMinutes">minutes</p><br />
<p id="seconds">00</p>
<p class="timeRefSeconds">seconds</p><br />
</div>
<!--<img src="images/incident_03.png" width="1220" height="388" alt="">--></td>
<td rowspan="2">
<img src="images/incident_04.png" width="86" height="492" alt=""></td>
</tr>
<tr>
<td>
<img src="images/incident_05.png" width="1220" height="104" alt=""></td>
</tr>
</table>
<!-- End Save for Web Slices -->
<?php $conn->close(); ?>
</body>
</html>
最佳答案
这个问题是我自己回答的。结果我从 sql 得到的结果是一个数组。通过更改以下内容解决了这个问题
$sql = "SELECT * FROM counter";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$counter1 = $row['date'];
}
} else {
echo "0 results";
}
关于javascript - 从 PHP Mysql 查询中将数据提取到 Javascript 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29539733/