2015-02-08 19 views
0

的下面的PHP數據庫查詢(從phpMyAdmin的)僅帶回一個值(第一個或最老的)轉換成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; 
     } 
    }  
} 
?> 

其他一切都看起來像它的工作正常。非常感謝您的幫助

回答

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 
    } 
} 
?> 
+0

感謝您的提示。您能否提供我需要做的確切更改? – Capamania 2015-02-08 16:11:42

+0

我可以用mysqli發佈代碼,這樣你就可以看到我的意思了,我將只刪除你返回數據的行。 – taliezin 2015-02-08 16:21:32

+0

那太棒了! – Capamania 2015-02-08 16:27:08