首先,我對PHP沒有任何經驗。我有2個陣列 - 最小壓力和最大壓力。他們從一個數據庫,並創建一個簡單的XML文件,你可以看到以下兩個訪問數據: -PHP - 數組元素問題
<pressuremaxhourly>1010,1009,1009,1009,1008,1008,1007,1005,1004,1003,1002,1002,1002,1002,1002,1002,1002,1002,1002,1002,1002,1001,1001,1001</pressuremaxhourly>
<pressureminhourly>1001,1009,1009,1008,1008,1006,1005,1004,1003,1002,1002,1002,1002,1002,1002,1002,1002,1002,1002,1001,1001,1001,1001,1001</pressureminhourly>
的問題是,你可以看到,最低數組的第一個值總是等於最後最大數組的值(在本例中爲「1001」)。這些數組每半小時被新數值覆蓋,但問題仍然存在。下面是我使用的代碼: -
$MaxPressure="select hour(datetime) AS hour, max(BarometricPressure) as MAXBP from minute WHERE DATETIME
BETWEEN (CURDATE() + INTERVAL (SELECT hour(NOW())) hour - INTERVAL 24 hour)
AND ((CURDATE() + INTERVAL (SELECT hour(NOW()))hour))
group by hour
order by (CURDATE() + INTERVAL (SELECT hour(NOW())) hour - INTERVAL 24 hour)";
$MaxPressureResult = mysql_query($MaxPressure) or die('Failed to query'.mysql_error());
while ($row = mysql_fetch_object($MaxPressureResult)) {
$maxpressure[]=$row->MAXBP;
}
$MinimumPressure="select hour(datetime) AS hour, min(BarometricPressure) as MINBP from minute WHERE DATETIME
BETWEEN (CURDATE() + INTERVAL (SELECT hour(NOW())) hour - INTERVAL 24 hour)
AND ((CURDATE() + INTERVAL (SELECT hour(NOW()))hour))
group by hour
order by (CURDATE() + INTERVAL (SELECT hour(NOW())) hour - INTERVAL 24 hour)";
$MinimumPressureResult = mysql_query($MinimumPressure) or die('Failed to query'.mysql_error());
while ($minrow = mysql_fetch_object($MinimumPressureResult)) {
$minimumpressure[]=$minrow->MINBP;
}
下面是用於創建XML文件中的代碼: -
$dom = new DOMDocument();
$root = $dom->createElement("weatherdata");
$dom->appendChild($root);
$item = $dom->createElement('item', "");
$root->appendChild($item);
$maxpressure = $dom->createElement('pressuremaxhourly', "$maxpressure[0],$maxpressure[1],$maxpressure[2],$maxpressure[3],$maxpressure[4], etc....for all values);
$item ->appendChild($maxpressure);
$minimumpressure = $dom->createElement('pressureminhourly', "$minimumpressure[0],$minimumpressure[1],$minimumpressure[2],$minimumpressure[3],$minimumpressure[4], etc....for all values);
$item ->appendChild($minimumpressure);
$dom->formatOutput = true;
$dom->save('Hourly.xml')
注:我已經在數據庫中檢查的數據,它是正確的,所以它的明確的腳本是這個問題。 對不起,長期的問題和代碼示例,希望有人可以提供幫助。
##### $$$$$更新$$$$$$ ####### 看來SQL腳本是沒有問題的。我將時間間隔設置爲24,並且在SQL工作臺中運行SQL腳本時,它將加載正確的結果。
但是,當它從上面的腳本運行時,第一個元素總是等於最後一個元素。 IE瀏覽器。 (minpressure [0] == minpressure [23])
還有什麼想法?
的SQL的正在返回正確的結果和陣列包含正確的元素嗎?你能檢查它嗎? –
好吧我已經運行了SQL,並且它們返回的結果與上面相同,所以它顯然是SQL腳本......同樣,對於SQL不是很有經驗,但我有一種感覺「小時 - INTERVAL 24小時」應該是23而不是? – mmmbaileys
是的,因爲您正在使用BETWEEN,它也會包含第一個和最後一個邊界。嘗試並檢查結果。 –