Search code examples
phpmysqlcsvdygraphs

Getting a csv file from mysql over PHP for dygraph.js


My Goal is to create a Website which displays data from my mysql database with dygraph.js. The website should load the data every time it is opened because my Mysql database receive new data every hour.

My Problem is that I dont know how I can make a PHP site that returns a csv file (not download it).

Basicly my Question is: Is it possible to connect mysql and dygraph? if yes, how?


Solution

  • Okay I got a solution.

    For everyone who has the same problem: I found a really nice tutorial from glaskugelsehen: https://glaskugelsehen.wordpress.com/2014/05/24/tutorial-speicherung-von-arduino-messdaten-auf-webserver-und-deren-darstellung-teil-5/

    Its german but you can use the code without understanding the language Because the tutorial is really old I had to translate it into mysqli and do some little changes. Then I ended up with this php code:

    <html>
        <head>
            <br>
            <script type="text/javascript"src="libraries/dygraph.min.js"></script>
            <link rel="stylesheet" src="CSS/dygraph.css" />
        </head>
        <body style="color: rgb(0, 0, 0); background-color: rgb(77, 77, 77)" alink="#ee0000" link="#0000ee" vlink="#551a8b">
            <div style="text-align: center;"><span style="color: rgb(255, 255, 255);">Daten aus MySQL<br></span>
                <table style="margin:0px auto" border="0" width="500" align="center">
                    <tr>
                        <td style="background-color: #FFFFFF">
                            <div id="graphdiv2"
                                 style="width:500px; height:300px;"></div>
                            <script type="text/javascript">
    
                                g2 = new Dygraph(document.getElementById("graphdiv2"),
                                                 <?php
                                                 $mysql_host = "****";
                                                 $mysql_db = "****";
                                                 $mysql_user = "****";
                                                 $mysql_pw = "****";
                                                 $connection = new mysqli($mysql_host, $mysql_user, $mysql_pw, $mysql_db);   // Check connection
                                                 if ($connection->connect_error) {
                                                     die("Connection failed: " . $connection->connect_error);
                                                 } 
                                                 $abfrage = "SELECT DATE_FORMAT(Zeit, '%Y/%m/%d %H:%i:%s') AS date, Temperatur, Luftfeuchtigkeit FROM messung";
                                                 $ergebnis = mysqli_query($connection,$abfrage);
    
                                                 echo "[";                                  // start of the 2 dimensional array
                                                 while($row = mysqli_fetch_array($ergebnis))
                                                 {
                                                     $utime = strtotime($row[datum]);
                                                     $diffutime = $utime - $lastutime;
                                                     if ($diffutime > 600) {
                                                         echo "["."new Date(\"".$row[date]."\")".",NaN,NaN],";
                                                     }
                                                     else {
                                                         echo "["."new Date(\"".$row[date]."\")".",".$row[Temperatur].",".$row[Luftfeuchtigkeit]."],";
                                                     }
                                                     $lastutime = strtotime($row[datum]);
                                                 }
                                                 echo "]";
                                                 mysqli_close($connection);
                                                 ?> ,
                                                 { rightGap: 20,
                                                 connectSeparatedPoints: true,
                                                 labels: [ "Zeit", "Temperatur", "Luftfeuchtigkeit" ] }          // options
                                                );
                            </script>
                        </td>
                    </tr>
                </table>
            </div>
        </body>
    </html>
    

    If somebody wants to use this code he has to fill in his mysql information.

    Hopefully this is helpful