php数组的问题

时间:2014-07-22 10:51:39

标签: php sql 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"

1 个答案:

答案 0 :(得分:3)

你在第一个片段中写了一个无限循环:

$rs = $this->Db($sql,"dtReading");
while ($rs) {
    $data[] = $this->DtReading = $rs;
}

Db方法返回结果。很好,但这意味着$rs不会是 falsy falsenull,空数组/字符串,...)。

然后,while条件始终为真,因此您不断将$rs的值推送到$data,这将增长并增长,直到不再有内存为止可以让它成长。

还有许多其他问题需要解决。简短列表:

  • subscribe to the coding standards
  • 在您的Db方法中:$result = $this->val = odbc_result($rs, $index);只是一遍又一遍地重新分配$result,我怀疑您希望$result成为数组
  • 在您的第一个代码段中:return round($this->time / 60, 2);位于while循环内。为什么要打扰一个不会循环的循环?
  • Google现有的数据库抽象层。那里有很多。花一些时间学习这些,而不是自己写。

回应您的评论(关于如何最好地循环结果):
我使用ODBC的经验相当有限,特别是我使用ODBC + PHP的经验(无任何exp)。但是,快速查看the docs会让我相信这是您开展业务的方式:

$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时要小心,noted on in the docs,如果您的查询字符串如下所示:

SELECT * FROM tbl;

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;
}

关于我在这里提到的每个功能的详细信息,check the manual,不要忘记玩得开心。