繁体   English   中英

从PHP Mysql查询将数据提取到Javascript中

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM