0

I had a php function to get all image from a blob field.

As below:

$db_corp = new PDO ("firebird:dbname=DATA.FDB;host=localhost", "sysdba", "masterkey");

$query_corp1= "Select CODPROD, ,FOTO From PRODUCT";

$result_corp1 = $db_corp->prepare($query_corp1);
$result_corp1->execute();
$result_corp1->bindColumn(1, $corp1_CODPROD);
$result_corp1->bindColumn(9, $corp1_FOTO, PDO::PARAM_LOB);

while($result_corp1->fetch()){
    if($corp1_FOTO){
        file_put_contents($corp1_CODPROD.".png",$corp1_FOTO);
    }
}

The script runs well and takes almost half of the photos in the database, but in the process i got following error.

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 480055 bytes)

I already tried gc_enable(); and gc_collect_cycles; My database has 2100 products but half have pictures and I need to extract it to a files.

Thanks in advancer

4
  • 1
    You can increase the memory_limit, which appears to be currently set to 128M Commented Apr 14, 2013 at 20:45
  • 1
    I can do it localy. But I don't have such privilege in my host Commented Apr 14, 2013 at 20:47
  • Your other option is to fetch data in pages. As you are fetching the result set, each row is getting stored in PHP memory space until you run out of memory. Commented Apr 14, 2013 at 20:52
  • From your question, I am unclear, do you get the error after the execute and during the while loop? Or do you get the error during the execute? I believe from how you state it that you get it during the loop, just want to clarify. Commented Apr 14, 2013 at 20:58

1 Answer 1

1

Rising memory usage in php.ini could help, but it's just a dirty fix and not a real solution. Try to retrieve 100 lines per time, than clean up some memory and start again... In "pseudocode":

$from = 1;
$to = 100;

while ($to < totalLines()) {
   getLines($from,$to);
   $from += 100;
   $to += 100;
   clearMemory();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much you gave me a great idea. Now I'm using ajax to iterate 100 lines per time and is working very well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.