尝试使用 odbc 从 Sybase 数据库获取数据时出现 HTTP ERROR 500

Getting HTTP ERROR 500 when trying to get data from Sybase database using odbc

我正在 CodeIgniter 框架上建立一个以前的 PHP 项目,我想使用 ODBC 从 SYBASE 数据库中获取一些数据。

当它不是很多数据时它工作正常但是当我需要获取大量数据时它只会给我一个 HTTP 错误 500。

在我以前的项目中,我不使用 CI,我没有得到这个错误,但我得到了一些 time_limit_error,我通过扩展 'max_execution_time' 修复了它;

我已经在 CI 上试过了,但它毫无意义,我从 CI 得到的错误并不具体,因为它只是说 'HTTP ERROR 500';

连接正确,因为它在我不需要大量数据时工作,但我没有使用 CI 标准,因为我也遇到了问题。

有谁知道我为什么会收到这个错误以及为什么我可以修复它?

我在 apache 日志中收到此错误:

[Fri Apr 12 14:49:03.573513 2019] [php7:error] [pid 11768:tid 1964] [client ::1:55425] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 35767312 bytes) in C:\xampp\htdocs\CodeIgniter\system\core\Output.php on line 198, referer: http://localhost/CodeIgniter/Samsic/provisoes 
[Fri Apr 12 14:49:03.574982 2019] [php7:error] [pid 11768:tid 1964] [client::1:55425] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 35770368 bytes) in Unknown on line 0, referer: http://localhost/CodeIgniter/Samsic/provisoes

打印页面:

> <?php ini_set("memory_limit", "256M");  ?> <!DOCTYPE html> <html>
> <head>    <title></title>     <style>     body{       font-size: 0.9em;   }
>   .container{         width: 100%;        height: 100%;   }
> 
>   @media print {      @page { margin: 20px; }         body { margin: 1.6cm; }     }
>   table, th, td {         border: 1px solid black;    </style> </head> <body
> onload="window.print()">  <?php $this->load->view('header'); ?>   <?=
> $resultado; ?>
> 
> </body> </html>

控制器:

<?php
ini_set("memory_limit", "256M");
defined('BASEPATH') OR exit('No direct script access allowed');

class Pagamentos extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('Pagamentos_model', '', TRUE);
        $this->load->library('session');
    }

    public function index(){
        $data['form'] = 'Pagamentosform';
        $this->load->view('norm',$data);

        }
    public function content(){
        $ano = addslashes($this->input->post('anop'));
        $session_data = array('anop' => $ano);
        $this->session->set_userdata($session_data);
        $codigo = addslashes($this->session->codigo);
        $resultado=$this->Pagamentos_model->read($codigo,$ano);
        $data['form'] = 'Pagamentosform';
        $data['res'] = 'Pagamentoscontent';
        $data['resultado'] = $resultado;
        $this->load->view('norm',$data);


    }

    public function imprimir(){
        $ano =  addslashes($this->session->anop);
        $codigo = addslashes($this->session->codigo);
        $resultado = $this->Pagamentos_model->imprimir($ano,$codigo);
        $data['resultado'] = $resultado;
        $this->load->view('imprimirTemplate',$data);


    }

    public function grafico(){
        $data['form'] = 'Pagamentosform';
        $data['res'] = 'grafico';
        $codigo = addslashes($this->session->codigo);
        $ano = addslashes($this->session->anop);
        $grafico = $this->Pagamentos_model->grafico($ano,$codigo);
        $data['data'] = $grafico;
        $this->load->view('norm',$data);


    }   

}

模型函数导入:

public function imprimir($ano,$codigo){
        require(APPPATH.'libraries/odbc_conn.php');
        $query = odbc_exec($db, 'Select * from GP_Vw_Valores_Pagos where Ano='.$ano.' and Codigo='.$codigo.' order by CD');
        $row=odbc_fetch_array($query);
        $output= '<h1 style="text-align: center;"> Pagamentos'.$ano.' </h1>  <table class="table" style="width: 100%; margin-bottom:40px; margin-top: 15px; ">
      <thead>
        <tr style="font-size: 1em;margin-bottom: 15px;text-align: center;">
          <th scope="col">CD</th>
          <th scope="col">Descrição</th>
          <th scope="col">Tipo</th>
          <th scope="col">Janeiro</th>
          <th scope="col">Fevereiro</th>
          <th scope="col">Março</th>
          <th scope="col">Abril</th>
          <th scope="col">Maio</th>
          <th scope="col">Junho</th>
          <th scope="col">Julho</th>
          <th scope="col">Agosto</th>
          <th scope="col">Setembro</th>
          <th scope="col">Outubro</th>
          <th scope="col">Novembro</th>
          <th scope="col">Dezembro</th>

        </tr> 
        </thead>';

        while($row){
            $output .= '<tr style="text-align: center;">
        <td>' .$row["CD"].'</td>
          <td>'.iconv("CP850", "UTF-8", $row['Descricao']).'</td>
          <td>VI</td>
          <td>' .number_format($row['Vl01'] , 2, '.', '').'</td>
          <td>' .number_format($row['Vl02'] , 2, '.', '').'</td>
          <td>' .number_format($row['Vl03'] , 2, '.', '').'</td>
          <td>' .number_format($row['Vl04'] , 2, '.', '').'</td>
          <td>' .number_format($row['Vl05'] , 2, '.', '').'</td>
          <td>' .number_format($row['Vl06'] , 2, '.', '').'</td>
          <td>' .number_format($row['Vl07'] , 2, '.', '').'</td>
          <td>' .number_format($row['Vl08'] , 2, '.', '').'</td>
          <td>' .number_format($row['Vl09'] , 2, '.', '').'</td>
          <td>' .number_format($row['Vl10'] , 2, '.', '').'</td>
          <td>' .number_format($row['Vl11'] , 2, '.', '').'</td>
          <td>' .number_format($row['Vl12'] , 2, '.', '').'</td>
               </tr>
               <tr style="text-align: center;">
        <td></td>
          <td></td>
          <td>QT</td>
          <td>' .number_format($row['Qt01'] , 2, '.', '').'</td>
          <td>' .number_format($row['Qt02'] , 2, '.', '').'</td>
          <td>' .number_format($row['Qt03'] , 2, '.', '').'</td>
          <td>' .number_format($row['Qt04'] , 2, '.', '').'</td>
          <td>' .number_format($row['Qt05'] , 2, '.', '').'</td>
          <td>' .number_format($row['Qt06'] , 2, '.', '').'</td>
          <td>' .number_format($row['Qt07'] , 2, '.', '').'</td>
          <td>' .number_format($row['Qt08'] , 2, '.', '').'</td>
          <td>' .number_format($row['Qt09'] , 2, '.', '').'</td>
          <td>' .number_format($row['Qt10'] , 2, '.', '').'</td>
          <td>' .number_format($row['Qt11'] , 2, '.', '').'</td>
          <td>' .number_format($row['Qt12'] , 2, '.', '').'</td>
               </tr>  ';
        }

        return $output;


    }

编辑

好的,您遇到了这个内存错误,因为您将整个 table 连接到内部的字符串变量和无限循环中,并尝试输出它。

请勿在模特身上这样做。

从您的模型中,您应该 return 视图上使用的值直接构建您的视图。

您在此代码中有很多修复:

第一次尝试

您可能 运行 内存不足,因为您说您正在加载大量数据。

尝试找到满足您需求的最佳价值

ini_set("memory_limit", "256M");

您需要 运行 在加载数据之前执行此操作,或者您可以将 php 设置更改为系统范围。