2015-12-01 179 views
2

我在整合Rgraph與PHP和MySQL數據時遇到問題。我遵循Rgraph網站的指示。如何將Rgraph與PHP和MySQL集成?

在Rgraph網站上,該示例使用數組數據,但我的情況不使用數組。 我想顯示有多少pegawai參加了一個月。

<?php 
$query2 = "SELECT count(id_absensi) AS jumhadir FROM absensi WHERE nip_pegawai = '123040269'"; 
if($query2){ 
    $data = array(); 

    while ($row = mysql_fetch_assoc($query2)){ 
     $data[] = $row["jumhadir"]; 
    } 

    $data_string = "[".join(",", $data)."]"; 
} else { 
    print('MySQL query failed with error : '.mysql_error()); 
} 

?> 
<html> 
<head> 

    <!-- Don't forget to update these paths --> 

    <script src="libraries/RGraph.common.core.js" ></script> 
    <script src="libraries/RGraph.line.js" ></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <script src="js/jquery-1.11.3.min.js"></script> 

</head> 
<body> 

    <canvas id="cvs" width="600" height="250">[No canvas support]</canvas> 
    <script> 
     chart = new RGraph.Line({ 
      id: 'cvs', 
      data: <?php print($data_string) ?>, 
      options: { 
       gutterLeft: 35, 
       gutterRight: 5, 
       hmargin: 10, 
       tickmarks: 'endcircle', 
       labels: <?php print("Kehadiran") ?> 
      } 
     }.draw() 
    </script> 

</body> 
</html>' 

我沒有收到任何錯誤,也沒有圖表。我錯過了什麼?

回答

1

此:

$query2 = "SELECT count(id_absensi) AS jumhadir FROM absensi WHERE nip_pegawai = '123040269'"; 

不運行查詢 - 它只是包含SQL statemennt的字符串。所以,你可以嘗試將其更改爲:

$sql = "SELECT count(id_absensi) AS jumhadir FROM absensi WHERE nip_pegawai = '123040269'"; 
$query2 = mysql_query($sql); 

if ($query2) { 
    // ... 

當然,你做一個查詢之前,您必須連接到數據庫:

$connection = mysql_connect('localhost', 'username', 'password'); 
mysql_select_db('myDatabase');