从PHP Mysql Query中将数据拉入Javascript

时间:2015-04-09 13:22:30

标签: javascript php mysql json

我正忙着为我们的一个办公室仪表板编写一个计时器,它计算自上次事件/重置以来的时间。

我有一个页面,其中一个高级管理员可以输入一个新的时间戳,然后用时间戳更新数据库中的表,我的下一页(我遇到的问题)连接到数据库,收集数据(通过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>

1 个答案:

答案 0 :(得分:0)

我自己回答了这个问题。事实证明,我从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";
}