php数据库查询(phpMyAdmin)只将一个值(第一个)带回amcharts

时间:2015-02-08 14:23:23

标签: php mysql amcharts

下面的php数据库查询(来自phpMyAdmin)只会将一个值(第一个或最旧的)带回到amcharts中:

<?php
class custom_class2 
{  
    var $charts;    // reference to the calling object.  

    function customfunction2($content,$conf)
    {   
        global $TSFE;        
        $TSFE->set_no_cache();

        // do whatever you want here

        // db connection
        mysql_connect("hostname", "username", "password");
        mysql_select_db("database name");

        //db abfrage
        $query = "
            SELECT 
                YEAR(datetime) AS dy, 
                MONTH(datetime) -1 AS dm, 
                DAY(datetime) AS dd, 
                HOUR(datetime) AS th, 
                MINUTE(datetime) AS tm, 
                temp, 
                hum, 
                pressure 
            FROM stock1 
            ORDER BY datetime
        ";

        // NEW: Variable definition 
        $zeilenzaehler = 1;

        // output of the rows
        $result = mysql_query($query) OR die("Error: $query <br>" . mysql_error());
        while ($row = mysql_fetch_array($result)) 
        {
            // return 
            if ($zeilenzaehler != 1) 
            {
                $content.= ",";
            }

            $content.= "{date: new Date(" . $row['dy'] . "," . $row['dm'] . "," . $row['dd'] . "," . $row['th'] . "," . $row ['tm'] . "),t:" . $row['temp'] . ",h:" . $row['hum'] . ",p:" . $row['pressure'] . "}";

            return $content;

            // Variable now on 2
            $zeilenzaehler = 2;
        }
    }      
} 
?>

其他一切看起来都很好。非常感谢您的帮助

1 个答案:

答案 0 :(得分:4)

您在while循环中返回第一个找到的结果。这就是为什么你只有一个结果。考虑切换到mysql_ *函数也是如此 mysqli_*PDO。 我正在添加您的请求中的代码:

<?php
class custom_class2
{
     var $charts;    // reference to the calling object.  

     function customfunction2($content,$conf)
     {
         global $TSFE;        
         $TSFE->set_no_cache();

         // do whatever you want here

         // db connection
         $mysqli = new mysqli("hostname", "username", "password", "database name");
         if ($mysqli->connect_error) {
             // your error handling here
         }

         //db abfrage
         $query = "
            SELECT 
                YEAR(datetime) AS dy, 
                MONTH(datetime) -1 AS dm, 
                DAY(datetime) AS dd, 
                HOUR(datetime) AS th, 
                MINUTE(datetime) AS tm, 
                temp, 
                hum, 
                pressure 
            FROM stock1 
            ORDER BY datetime
         ";

         // NEW: Variable definition 
         $zeilenzaehler = 1;

         // output of the rows
         $result = $mysqli->query($query);
         if (FALSE === $result) {
             // you can put different error handling here
             echo 'Error: ' . $query . ' ' . $mysql->error);
             die();
         }
         $total = array();
         while (NULL !== ($row = $result->fetch_array()))
         {
             // return
             if ($zeilenzaehler != 1)
             {
                 $content.= ",";
             }

             $content.= "{date: new Date(" . $row['dy'] . "," . $row['dm'] . "," . $row['dd'] . "," . $row['th'] . "," . $row ['tm'] . "),t:" . $row['temp'] . ",h:" . $row['hum'] . ",p:" . $row['pressure'] . "}";

             // return $content;
             // if you not return the first result you can gather results in array, so array will contain every row in result, $total[0], $total[1]...:
             // $total[] = $content; or:
             $total[] = "{date: new Date(" . $row['dy'] . "," . $row['dm'] . "," . $row['dd'] . "," . $row['th'] . "," . $row ['tm'] . "),t:" . $row['temp'] . ",h:" . $row['hum'] . ",p:" . $row['pressure'] . "}";

             // Variable now on 2
             $zeilenzaehler = 2;
         }

         $result->free();
         return $total; // return all rows
     }
 }
 ?>