2016-08-19 96 views
1

我在创建图表时使用mysql数据库和php时出现问题。从图表中的数据库中选择不更新值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="[email protected]"; 
     $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没有更新这些值。

有什么想法?

在此先感谢。

回答

0

问题DrawGraph简单绘制与当页面第一加载

,以获得新的数据到页面提供的数据图表,你需要重新加载窗口PHP运行并写入新的数据页面

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

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

+0

理想情况下,你想单独的PHP和HTML/JavaScript的 - 有PHP返回只有数据,然后使用JavaScript/AJAX从PHP获取数据 - 虽然不同的图表库,[这里是一个例子](http ://stackoverflow.com/questions/38947898/redraw-google-chart-based-on-user-input-via-ajax-request/38955110#38955110) – WhiteHat

+0

这个问题的运气? – WhiteHat

相关问题