php数组的问题

php数组的问题,php,sql,arrays,Php,Sql,Arrays,您好,我现在正在创建一个脚本,从odbc连接中获取数据并将其存储在一个数组中。当我遇到此错误时,我有另一个名为data的函数,它做同样的事情,但不将数据放入数组中,并且该函数工作正常。我不确定我做错了什么,希望您能提供帮助 Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes) in 我有一个类似的函数,循环函数,但它没有把数据放到数组中,它在这里工作得很好,这就是代码

您好,我现在正在创建一个脚本,从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 connect函数

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_转储

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\u result($rs,$index)
    只是一遍又一遍地重新分配
    $result
    ,我怀疑您希望
    $result
    成为一个数组
  • 在您的第一段代码中:
    returnround($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\u fetch\u array
时,如果查询字符串如下所示,请务必小心:

SELECT * FROM tbl;
odbc\u fetch\u 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;
}

有关我在这里提到的每个函数的详细信息,请不要忘记玩得开心。

您在第一段代码中都编写了一个无限循环:

$rs = $this->Db($sql,"dtReading");
while ($rs) {
    $data[] = $this->DtReading = $rs;
}
Db
方法返回一个结果。很好,但这意味着
$rs
不会出错(
false
null
,空数组/字符串,…)

因此,
while
条件始终为真,因此您不断将
$rs
的值推送到
$data
,该值将不断增长,直到没有更多内存可供其增长

还有许多其他问题需要解决。短名单:

  • Db
    方法中:
    $result=$this->val=odbc\u result($rs,$index)
    只是一遍又一遍地重新分配
    $result
    ,我怀疑您希望
    $result
    成为一个数组
  • 在您的第一段代码中:
    returnround($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\u fetch\u array
时,如果查询字符串如下所示,请务必小心:

SELECT * FROM tbl;
odbc\u fetch\u 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;
}

有关我在这里提到的每个函数的详细信息,请不要忘记玩得开心。

您在第一段代码中都编写了一个无限循环:

$rs = $this->Db($sql,"dtReading");
while ($rs) {
    $data[] = $this->DtReading = $rs;
}
Db
方法返回一个结果。很好,但这意味着
$rs
不会出错(
false
null
,空数组/字符串,…)

因此,
while
条件始终为真,因此您不断将
$rs
的值推送到
$data
,该值将不断增长,直到没有更多内存可供其增长

还有许多其他问题需要解决。短名单:

  • Db
    方法中:
    $result=$this->val=odbc\u result($rs,$index)
    只是一遍又一遍地重新分配
    $result
    ,我怀疑您希望
    $result
    成为一个数组
  • 在您的第一段代码中:
    returnround($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\u fetch\u array
时,如果查询字符串如下所示,请务必小心:

SELECT * FROM tbl;
odbc\u fetch\u 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;
}
有关每个功能的详细信息