J
J
John Freeman2017-05-09 13:12:19
PHP
John Freeman, 2017-05-09 13:12:19

Why does a 500 Internal Server Error occur? How to fix?

I have a database of more than 2000 records and will be replenished!
on the page I display a table, 10 records per page + pagination everything is ok.
I made a button (file) for export!
here is the code for that pastebin file:
https://pastebin.com/FgQv3nZT
and here:

spoiler
<?php
ini_set("max_execution_time", "3600"); // ожидаем

// // Игнорирует отключение пользователя и позволяет скрипту
// // быть запущенным постоянно
// ignore_user_abort(true);
// ini_set('error_reporting', E_ALL);
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
//require "auth.php";
//Инклюдим файл подключения с базой данных
include_once($_SERVER["DOCUMENT_ROOT"].'/db.php');


  $getsort = $_GET['sort'];
if ($getsort === 'desc'){
  $sort = 'desc';
}else{
  $sort = 'asc';
}
  $getkey = $_GET['key'];

if (!empty($getkey)) {
  $key = $getkey;
}else{
  $key = 'id';
}

$q=$db->query('SELECT * FROM `news` WHERE `show_in_feed`=1 ORDER BY `'.$key.'` '.$sort.' ');

require('classes/PHPExcel.php');
// Подключаем класс для вывода данных в формате excel
require_once('classes/PHPExcel/Writer/Excel5.php');

// Создаем объект класса PHPExcel
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array( 'memoryCacheSize ' => '256MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings); 

    $pExcel = new PHPExcel();
    $pExcel->setActiveSheetIndex(0);
    $aSheet = $pExcel->getActiveSheet();
    $aSheet->setTitle('List');
        //добавление данных
        $aSheet->setCellValue("A1", "Added Date");
        $aSheet->setCellValue("B1", "Title");
        $aSheet->setCellValue("C1", "Threat Type");
        $aSheet->setCellValue("D1", "Domain");
        $aSheet->setCellValue("E1", "URL");
 


$rowCount = 2;

while($n=$q->fetch_assoc()){

$type_ed = explode(",", $n['threat_type']);
$type_edit = '';
foreach ($type_ed as $type_edd)
{
        $ttt = $db->query("SELECT * FROM `type` WHERE `id`='".$type_edd."'")->fetch_assoc();
        $name_type = $ttt['type'];
        $type_edit.= "$name_type,";
        
}
$threat_type = substr($type_edit, 0, -1);

        $aSheet->setCellValue("A".$rowCount, $n["added_date"]);
        $aSheet->setCellValue("B".$rowCount, $n["title"]);
        $aSheet->setCellValue("C".$rowCount, $threat_type);
        $aSheet->setCellValue("D".$rowCount, $n["domain"]);
        $aSheet->setCellValue("E".$rowCount, $n["full_url"]);


  $rowCount++;

}
        //форматирование
        $style_wrap = array( 'font' => array( 'bold' => true ) );
        $aSheet->getStyle('A1:V1')->applyFromArray($style_wrap);
        //отдаем пользователю в браузер
    // include("classes/PHPExcel/Writer/Excel2007.php");
    // $objWriter = new PHPExcel_Writer_Excel2007($pExcel);
     $objWriter = new PHPExcel_Writer_Excel5($pExcel);
    ob_clean();

    // header('Cache-Control: max-age=0');
 header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" );
 header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" );
 header ( "Cache-Control: no-cache, must-revalidate" );
 header ( "Pragma: no-cache" );
 header ( "Content-type: application/vnd.ms-excel" );
 header ( "Content-Disposition: attachment; filename=List.xls" );
    $objWriter->save('php://output');

?>


When I click on the export button, the script thinks and gives an error 500 Internal Server Error
Help please, what am I doing wrong?!
PS: If I export only 10-50 records, then everything is OK, it saves the xls file
Update:
I removed the code:
type_ed = explode(",", $n['threat_type']);
$type_edit = '';
foreach ($type_ed as $type_edd)
{
        $ttt = $db->query("SELECT * FROM `type` WHERE `id`='".$type_edd."'")->fetch_assoc();
        $name_type = $ttt['type'];
        $type_edit.= "$name_type,";
       
}

the error disappeared and everything was exported, now the question is how can I get the names from the type table when exporting??

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
A person from Kazakhstan, 2017-05-09
@LenovoId

https://www.ipipe.ru/info/oshibka-500.html

T
ThunderCat, 2017-05-09
@ThunderCat

1) There is a log where all errors are written, IT IS NECESSARY TO LOOK, to look for a solution at the level of "something does not work, you have to poke your finger in different places" is stupid and inefficient.
2) If the log is not kept (which is of course strange, but possible), then the error logs must be enabled, the output can be omitted.
3)

type_ed = explode(",", $n['threat_type']);
$type_edit = '';
foreach ($type_ed as $type_edd)
{
        $ttt = $db->query("SELECT * FROM `type` WHERE `id`='".$type_edd."'")->fetch_assoc();
        $name_type = $ttt['type'];
        $type_edit.= "$name_type,";
       
}

something in this piece is wrong, requests in the cycle are evil, and here a more different solution clearly suggests itself, offhand it was possible to make 1 request at once where `id` in ($ type_ed), although I suspect that this is a crookedly organized list of categories written by a line with commas as separator. This is g*nokod, that's why there are brakes and glitches.
4) If the problem is in the script execution time, then you can run it in console mode in a separate process in the background, which is normal for one-time tasks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question