0

How can I store a generated TCPDF as a blob in MYSQL? I tried:

   $file = new TCPDF('p', 'mm', 'A4');
   $file->AddPage();
   $file->writeHTMLCell(190, 0, '', '', '<span>some html content</span>');

   $pdf = addslashes($file);

   $query = 'INSERT INTO '.$this->table.' (file) VALUES ("'.$pdf.'")';

   $stmt = $this->conn->prepare($query);

   $stmt->execute()

but it stores empty string.

Ark Luis
  • 275
  • 3
  • 14
  • 1
    Try it without the addslashes , you store binary data, not strings. Next use **prepared statements** https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php and an error rjandling as well. – nbk Apr 06 '20 at 18:52
  • then im getting Object of class TCPDF could not be converted to string – Ark Luis Apr 06 '20 at 19:32
  • What is file for an dataype and have you used **prepared statements** yet – nbk Apr 06 '20 at 19:34
  • an instance of TCPDF – Ark Luis Apr 06 '20 at 19:38
  • no the database datatype, you are adding a string but blob is a binary – nbk Apr 06 '20 at 19:43
  • database datatype is a regular blob, but even if a pass a object of type TCPDF (without any addslashes or other string conversion) im getteing that error – Ark Luis Apr 06 '20 at 19:49
  • I hope you can learn from the error you are getting, that it is impossible to save an object in the database. – Your Common Sense Apr 06 '20 at 20:07
  • i am not sure if that object is intended to be save,d even when you make it to an string. I really don't know, if you could used at all. $pdf = $file.Outout(test'pdf','S') gives you an an string that you could save Maybe you can experiment with that. – nbk Apr 06 '20 at 20:34
  • i finally found the solution, but it wasn't directly given in already answered issue: $pdf = $file->Output('', 'S'); $pdf = addslashes($pdf); – Ark Luis Apr 07 '20 at 06:44

0 Answers0