![](/img/trans.png)
[英]D3 javascript pulling data from mysql Uncaught typeError
[英]Pulling data into Javascript from PHP Mysql Query
我正忙於為我們的一個辦公室儀表板編寫一個倒數計時器,該倒數計時器用於計算自上次事件/重置以來的時間。
我有一個頁面,其中一位高級管理員可以輸入新的時間戳,然后用時間戳更新數據庫中的表,我的下一頁(我遇到問題的頁面)連接到數據庫,並通過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";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.