javascript - 从图表中的数据库中选择不更新值 php

标签 javascript php mysql charts rgraph

我在创建图表时使用 mysql 数据库和 php 时遇到问题。

这是我的代码:

<?php session_start(); ?>
<!DOCTYPE html > 
<html> 
<head> 
    <link rel="stylesheet" href="demos.css" type="text/css" media="screen" /> 
    <script src="RGraph/libraries/RGraph.common.core.js" ></script> 
    <script src="RGraph/libraries/RGraph.line.js" ></script> 

    <title>Line chart with scrolling effect</title> 

    <meta name="robots" content="noindex,nofollow" /> 
    <meta name="description" content="A chart which updates itself every 250ms producing a scrolling effect" /> 

</head> 
<body> 

    <h1>Line chart with scrolling effect</h1> 

    <canvas id="cvs" width="600" height="250">[No canvas support]</canvas> 

    <script> 
        data = RGraph.arrayPad([], 500); 
        function DrawGraph () 
        {

            RGraph.clear(document.getElementById("cvs")); 
            RGraph.ObjectRegistry.clear(); 

            var line = new RGraph.Line({ 
                id: 'cvs', 
                data: data, 
                options: { 
                    colors: ['green'], 
                    linewidth: 1, 
                    filled: true, 
                    fillstyle: 'rgba(128,255,128,0.5)', 
                    ymax: 60, 
                    tickmarks: null, 
                    shadow: false, 
                    numxticks: 5,
             labels: ['0s','5s','10s','15s','20s','25s'], 

                 //   labels: ['Now','25s','50s','75s','100s','125s'], 
                    backgroundGridAutofitNumvlines: 5, 
                    textAccessible: true 
                } 
            }).draw() 

<?php

$host="localhost"; 
        $user="root"; 
        $pass="P@ssw0rd"; 
        $dbh = new PDO("mysql:host=$host;dbname=VGW", $user , $pass );

$req="SELECT CPUUtilization from G_info where login_id='".$_SESSION['login_device_id']."' limit 30;";
$stmt=$dbh->query($req);
if(!$stmt)
{
  echo "Lecture impossible";
}
else
{
  $nblig=$stmt->rowCount();
  $ligne=$stmt->fetchObject();
 if ($ligne != null) {
  do
  {
$f=$ligne->CPUUtilization;
?>
 r  = <?php echo $f; ?>; data = [r].concat(data);data.pop(); 
           <?php
  }
  while ($ligne = $stmt->fetchObject()) ;
 }
else
{echo "<br> \n No such result";}

 $stmt->closeCursor();
 $dbh=null; 
  }
  ?>   
            setTimeout(DrawGraph, 2500); 
        } 

        window.onload = function () 
        { 
            DrawGraph(); 
        }; 
    </script> 
</body> 
</html>

问题是在图表的输出中:

如果修改了数据库(表g_info)中的30个值,则查询返回与函数中第一个条目相同的结果。 $req 似乎没有更新值。

有什么想法吗?

提前致谢。

最佳答案

问题是 DrawGraph 只是使用页面第一次加载时提供的数据绘制图表

要获取新数据到页面,需要重新加载窗口让php运行并写入新数据到页面

而不是...
setTimeout(DrawGraph, 2500);

你可以试试...
setTimeout(function () {window.location.reload();}, 2500);

关于javascript - 从图表中的数据库中选择不更新值 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39034633/

相关文章:

php - 如果 URL 后没有 $_POST 存在,如何防止(没有任何内容)传递到 MySQL 查询中并导致错误?

mysql选择100公里以内的数据

javascript - 序列化数组

javascript - 结构化函数。无法在现有代码中添加 if 语句

javascript - 如何在 Try-Catch block 中围绕错误打印行号

php - 使用 PDO 计算每个页面加载的查询数

php - 我可以将 heroku 连接到外部 MySQL 服务器吗?

javascript - 统计一秒钟内鼠标点击的次数

php - 在 Woocommerce 中将 Avada Catalog 排序 Hook 覆盖回默认值

mysql - 使用 Solr 存储数据以供检索 Vs。 MySQL