2017-08-12 129 views
-1

我試圖在一個時間範圍內創建一組溫度讀數,但每10個讀數將其限制爲只有1個讀數。可以幫助我將這個函數添加到下面的代碼中嗎? :)MySQL將時間範圍限制在10個間隔內

$dataArray=array(); 

$sql="SELECT * FROM $sensorID WHERE Time > DATE_SUB(NOW(), INTERVAL $timeDuration HOUR) AND Time <= NOW()"; 

$result = mysql_query($sql) or die('Query failed: ' . mysql_error()); 

if ($result) { 
    while (
    $row = mysql_fetch_array($result)) { 
    $time=$row["Time"]; 
    $temperature=$row["Temp"]; 
    $dataArray[$time]=$temperature; 
    }} 

乾杯

+1

編輯你的問題,並提供樣本數據和預期的結果。 –

+1

請停止使用PHP的棄用的mysql API – Strawberry

回答

0

如果我理解正確的話,你想顯示每10行?

$i=0; 
if ($result) { 
    while (
    $row = mysql_fetch_array($result)) { 
    if($i % 10 == 0) 
    { 
     $time=$row["Time"]; 
     $temperature=$row["Temp"]; 
     $dataArray[$time]=$temperature; 
    } 
    $i++; 
    }} 
+0

是的,我需要每隔10行,我已經嘗試過你的代碼,但它不適用於我: - / –

0

感謝邁克爾O.

它爲我現在的工作:d

$i=0; 
if ($result) { 
while (
$row = mysql_fetch_array($result)) { 
if($i % 10 == 0) 
{ 
$time=$row["Time"]; 
$temperature=$row["Temp"]; 
$dataArray[$time]=$temperature; 
} 
$i++; 
}} 
相關問題