php - Simple HTML DOM Memory issue -


i'm trying use php simple html dom parser parse information sql query results. seems, there huge memory problem it. create html table using sql query results , export html table csv file. new code not efficient one. when query results small csv file created successfully. when query results large, exported csv file not have sql results , instead shows :

fatal error: call member function find() on boolean in /opt/lampp/htdocs/test.php on line 101

this function takes sqlresult , creates html table , exports csv file:

 echo sql_to_html_table($sqlresult, $delim="\n" );  function sql_to_html_table($sqlresult, $delim="\n") { // starting table include_once('simple_html_dom.php'); $htmltable =  "<table>" . $delim ; $counter   = 0 ; // putting in lines //while( $row = $sqlresult->mysqli_fetch_assoc()  ){ while($row = mysqli_fetch_assoc($sqlresult)) { if ( $counter===0 ) { // table header $htmltable .=   "<tr>"  . $delim; foreach ($row $key => $value ) {       $htmltable .=   "<th>" . $key . "</th>"  . $delim ;   }   $htmltable .=   "</tr>"  . $delim ;   $counter = 22;  }   // table body   $htmltable .=   "<tr>"  . $delim ;   foreach ($row $key => $value ) {       $htmltable .=   "<td>" . $value . "</td>"  . $delim ;   }   $htmltable .=   "</tr>"   . $delim ;  }  // closing table  $htmltable .=   "</table>"   . $delim ;  // return //return( $htmltable ) ; $html = str_get_html($htmltable);  header('content-type: application/ms-excel'); header('content-disposition: attachment; filename=sample.csv');  $fp = fopen("php://output", "w");  foreach($html->find('tr') $element) { $td = array(); foreach( $element->find('th') $row) {     $td [] = $row->plaintext; } fputcsv($fp, $td); $td = array(); foreach( $element->find('td') $row) {     $td [] = $row->plaintext; } fputcsv($fp, $td); } fclose($fp); }  

i have tried throwing exception after $html = str_get_html($htmltable); this:

if (!str_get_html($htmltable)) { throw new exception('exception') ;  } 

and when try run code browser gives me error:

fatal error: uncaught exception 'exception' message 'exception' in /opt/lampp/htdocs/test.php:96 stack trace: #0 /opt/lampp/htdocs/test.php(62): sql_to_html_table(object(mysqli_result), '\n') #1 {main} thrown in /opt/lampp/htdocs/test.php on line 96

looking @ copy of simple_html_dom.php sourceforge, sounds expected behavior sufficiently big html string. see str_get_html() has check cause return false if size of string greater max_file_size. , max_file_size defined with:

define('max_file_size', 600000); 

so looks simple_html_dom won't handle string bigger 600kb. since that's built-in limitation, guess options either try change limit , see happens or use different library.

alternatively, skip html portion altogether. if need generate html other purposes, that's fine, there's no reason can't bypass problem building csv directly database results rather html.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -