How to integrate Rgraph with PHP and MySQL?

I'm having a problem integrating Rgraph with PHP and MySQL data. I followed instructions from the Rgraph site.

On the Rgraph site, the example uses array data but my case does not use array. I want to display how many pegawai attended for a month.

<?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>'

I'm not getting any errors and I've got no graph. What am I missing?

Upvotes: 2

Views: 895

Answers (1)

Richard
Richard

Reputation: 5101

This:

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

Doesn't run the query - its just a string that contains the SQL statemennt. So you could try changing it to:

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

if ($query2) {
    // ...

Of course before you do a query you must connect to your database:

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

Upvotes: 1

Related Questions