Menu

Digitemp Mysql Questions

2008-11-24
2012-09-07
  • mark stokes

    mark stokes - 2008-11-24

    Sorry if this is more of a MySQL related ask but I'm not a very strong coder and my head is starting to spin.
    I log some temperature readings using digitemp to a MySQL database. I have GD and php setup correctly and can plot simple graphs using some code I hacked from elsewhere however the format is pretty pants hence my attraction to PHPlot.
    I seem to be running into trouble with fetching my data to an array that PHPlot can read, if anybody could correct my code I'd greatly appreciate the help.

    I'm only looking to create a simple line graph so far... I'll work myself up to a multiple line graph later.

    code is....

    <?php
    include 'phplot.php';
    include 'config.php';
    include 'opendb.php';

    $query = "SELECT time, Fahrenheit, SerialNumber FROM digitemp WHERE SerialNumber='280B428F01000064' ORDER BY time DESC LIMIT 12";
    $result = mysql_query($query);

    $example_data = array();

    while($row = mysql_fetch_row($result))
    {
    $example_data[] = array("",$row"time", $row"Fahrenheit");
    }

    $graph =& new PHPlot();
    $graph->SetDataValues($example_data);
    $graph->DrawGraph();

    include 'closedb.php';
    ?>

    thanks in advance,

    Mark

     
    • Steve Charles

      Steve Charles - 2008-11-25

      Hi Mark

      When you build the example_data array I think you need to refer to the position in $row with a number because it is an array itself. Like:

      $example_data[] = array("",$row[0], $row[1]);

      Also if the time field in your database is a time data type, it'll return a text value, so you'll probably be better off with the following:

      $example_data[] = array("",strtotime($row[0]), $row[1]);

      and add these lines before your call to DrawGraph:

      $graph->SetXLabelType('time');
      $graph->SetXTimeFormat("%H:%M");

      Hope this helps, I'm new too!

       
    • lbayuk

      lbayuk - 2008-11-25

      With the right query, you can copy the mysql_fetch_row() results right into an array for plotting (for example: SELECT '', y1_val, y2_val FROM ...) But however you do this, have some way to look at the data array you are building. It can save a lot of time troubleshooting to be able to print_r() the array, instead of plotting it, so you can see what you are actually getting. And make sure you will see errors (check results from the database, check web server error logs or run your scripts from CLI first).

      This:
      example_data[] = array("",$row"time", $row"Fahrenheit");
      Is wrong, as the above post says, because $row is an array, but also what does $row"time" mean? It isn't valid PHP.

       
  • racheney

    racheney - 2010-02-21

    I am having the same problem,

    using the sample above with the correction mentioned (some data changed of
    course),

    all i get when I echo $example_data is

    array

    no data with it and i have 43 entries in the database table, if i do a loop i
    get

    arrayarrayarrayarrayarrayarrayarrayarrayarray

    but if I echo $result I get

    Resource id #2

    any help

    thank you

    racheney

     
  • lbayuk

    lbayuk - 2010-02-21

    Read my (#3) post above. You need to look at your data array, to see if it is
    in the right format for a PHPlot data array. Echo will not help with an array,
    as you found. You need to use print_r() or var_dump() for example.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.