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

标签 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;
        }
    }      
} 
?>

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

最佳答案

您在 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
     }
 }
 ?>

关于php 数据库查询 (phpMyAdmin) 只将一个值(第一个)带回 amcharts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28394988/

相关文章:

java - 为什么日期没有保存到我的数据库中?

从数组和变量过滤时PHP mysql查询问题

javascript - 在 AmCharts 中隐藏工具提示

javascript - amCharts map ,纬度对数坐标上带有气泡

javascript - Amcharts - 堆积柱形图/饼图 - 减少其他堆栈/饼图的不透明度

php - Laravel 5.2 artisan 优化 - php_strip_whitespace 无法打开流 : No child processes

php - Node.js 不接受 mongodb 中数据库的间接身份验证

python - 如何从 python mysql 查询中转义 %

php - Codeigniter 和 SQL/Haversine 公式

php - 通过 phpMyAdmin 更改表中的 URL