[英]issues with php arrays
嗨,我目前正在創建一個腳本,以從odbc連接中獲取數據,並在出現此錯誤時將其存儲在數組中,我還有另一個名為Data的函數,該函數執行相同的操作,但不會將數據放入數組中和那個很好,我不確定我做錯了什么,將不勝感激。
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes) in
我有一個與此功能類似的功能,但它沒有將數據放入數組中,並且在這里代碼非常有效
Public function Data() {
$sql = "SELECT TOP 1 ReaderData.ReaderIndex, ReaderData.CardID, ReaderData.ReaderDate, ReaderData.ReaderTime, ReaderData.controllerID, Left([dtReading],10) AS [date], ReaderData.dtReading FROM ReaderData
WHERE ReaderData.controllerID=$this->Id AND ReaderData.CardID = 'FFFFFFF0 '
ORDER BY ReaderData.ReaderIndex DESC;";
$rs = $this->Db($sql,"dtReading");
while ($rs) {
$this->DtReading = $rs;
$result = strtotime($this->DtReading) + 2 * 60 * 60;
$this->time = time() + 60 * 60 - $result;
return round($this->time / 60, 2);
}
}
這是有錯誤的功能
public function Cycle() {
$sql = "SELECT TOP 2 ReaderData.ReaderIndex, ReaderData.CardID, ReaderData.ReaderDate, ReaderData.ReaderTime, ReaderData.controllerID, Left([dtReading],10) AS [date], ReaderData.dtReading FROM ReaderData WHERE ReaderData.controllerID=$this->Id AND ReaderData.CardID = 'FFFFFFF0 ' ORDER BY ReaderData.ReaderIndex DESC;";
$rs = $this->Db($sql,"dtReading");
$data = array();
while ($rs) {
$data[] = $this->DtReading = $rs;
}
// var_dump($data);
$this->Time1 = ($data[0]);
$this->Time2 = ($data[1]);
$Time1E = strtotime($this->Time1) + 2 * 60 * 60;
$Time2E = strtotime($this->Time2) + 2 * 60 * 60;
$this->cycle = $Time1E - $Time2E;
return round($this->cycle, 2);
這是Db連接功能
Public function Db($sql,$index) {
$conn = new database_odbc('monitor', '', '');
$rs = $conn->justquery($sql);
while (odbc_fetch_row($rs)) {
$result = $this->val = odbc_result($rs, $index);
return $result;
}
這是循環功能中$ rs的Var_dump
string(23) "2014-07-22 06:20:30.000"
.......string(23) "2014-07-22 11:49:00.000"
.......string(23) "2014-07-22 11:52:26.000"
.......string(23) "2014-07-22 10:48:59.000"
.......string(23) "2014-07-22 11:52:24.000"
.......string(23) "2014-07-22 11:49:09.000"
.......string(23) "2014-07-22 11:52:30.000"
.......string(23) "2014-07-22 11:53:30.000"
.......string(23) "2014-07-22 11:54:53.000"
.......string(23) "2014-07-22 11:52:38.000"
.......string(23) "2014-07-19 13:19:21.000"
.......string(23) "2014-07-22 11:23:58.000"
.......string(23) "2014-07-21 13:33:06.000"
.......string(23) "2014-06-23 11:15:43.000"
您在前兩個代碼段中都編寫了一個無限循環:
$rs = $this->Db($sql,"dtReading");
while ($rs) {
$data[] = $this->DtReading = $rs;
}
Db
方法返回結果。 很好,但這意味着$rs
不會是虛假的 ( false
, null
,空數組/字符串,...)。
因此, while
條件始終成立,因此您將$rs
的值不斷推到$data
,它將不斷增長,直到沒有更多的可用內存來增長為止。
還有許多其他問題需要解決。 簡短清單:
Db
方法中: $result = $this->val = odbc_result($rs, $index);
只是一遍又一遍地重新分配$result
,我懷疑您希望$result
是一個數組 return round($this->time / 60, 2);
在while
循環內。 為什么要麻煩一個不會循環的循環? 回應您的評論(關於如何最好地遍歷結果):
我對ODBC的經驗非常有限,尤其是對ODBC + PHP的經驗(無經驗)。 但是快速瀏覽一下文檔會使我相信這是開展您的業務的方式:
$res = odbc_exec($connection, 'QUERY');
if (!$res)
throw new RuntimeException('Query failed');
$result = array();//array of results
while($row = odbc_fetch_array($res))
{
$result[] = $row;
}
但是,在使用odbc_fetch_array
,請務必小心,如docs中所述 ,如果您的查詢字符串如下所示:
SELECT * FROM tbl;
odbc_fetch_array
返回的odbc_fetch_array
將不是關聯數組。 SELECT *
之外的所有東西無論如何都是不好的形式,您不應該使用它,但是如果您堅持使用,那么更安全(但更冗長)的方式將是:
$res = odbc_exec($connection, 'QUERY');
if (!$res)
throw new RuntimeException('Query failed');//use odbc_error and odbc_errormsg!
//according to the docs, though index starts at 1, you need to do this:
$idx = 0;
$result = array();
odbc_fetch_row($res, $idx++);
while (odbc_fetch_row($res, $idx++))
{
$row = array(
'field1' => odbc_result($res, 'field1'),
'field2' => odbc_result($res, 'field2'),
'field3' => odbc_result($res, 3),//field number is fine, too
);
$result[] = $row;
}
有關我在這里提到的每個功能的詳細信息, 請查閱手冊 ,不要忘了玩樂。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.