【问题标题】:php database query (phpMyAdmin) only brings back one value (the first one) into amchartsphp数据库查询(phpMyAdmin)只将一个值(第一个)带回amcharts
【发布时间】:2015-02-08 14:23:23
【问题描述】:

下面的 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;
        }
    }      
} 
?>

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

【问题讨论】:

    标签: php mysql amcharts


    【解决方案1】:

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

    【讨论】:

    • 感谢您的提示。您能否提供我需要做的确切更改?
    • 我可以使用 mysqli 发布代码,这样您就可以明白我的意思,我将删除您返回数据的行。
    • 不幸的是它不起作用。我使用 Typo3,我认为它需要根据文档 (docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/…) 以“return”返回
    • 这是错误信息:“错误: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"
    • 我认为错误在查询中,这是你死的地方 $mysql->query 或 die()... 最后你可以返回整个数组 $total。
    猜你喜欢
    • 1970-01-01
    • 2022-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2021-09-07
    • 2021-06-27
    • 1970-01-01
    相关资源
    最近更新 更多