-1

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?

Julius
  • 21
  • 7
  • You can't mix MySQL APIs like that. mysql_* and mysqli_* don't mix. – aynber Jan 11 '19 at 18:48
  • Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Dharman Jan 11 '19 at 18:49
  • Yeah. Thats because this is a script which i build from a few code snippets. It worked for me and I didnt find a better solution. If you know one, it would be great if you share it with me. – Julius Jan 11 '19 at 18:54
  • What specific problem are you having? I think AJAX ignores the `Content-disposition: attachment` header, so the CSV file should be returned to dygraph. If not, try just leaving out that header. – Barmar Jan 11 '19 at 20:48

1 Answers1

0

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

Julius
  • 21
  • 7