title: PHP to PDF from MySQL Database
body: <p>I have read up and tried multiple tutorials and each time I run into a block I cannot pass.</p>
  
  <p>I have a database with 59 fields currently, I have a form agents fills in their sales into each day. Each day from 15-50 sales are captured. Currently I have a HTML Template where I have populated with the variables of the database fields and I run it through a loop displaying all those sales each in a separate table, then I print to pdf each sale Table on a separate page. You can imagine how labour intensive this is every day.</p>
  
  <p>I am now trying to convert each loop run which will be a record of a sale into their own pdf and name the pdf file according to a mix of variables from the database table.</p>
  
  <p>I cannot get the php variables to work in the PDF Generator. I can display the Data in the table, and generate the pdf off my template, but as soon as I add the php mysql database field variables I keep getting errors about he variables and the pdf gereator fails.</p>
  
  <p>Here is what I have done:</p>
  
  <p>TCPDF:</p>
  
  <pre><code>&lt;?php
  
  $pdf = new TCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false);
  
  // set document information
  $pdf-&gt;SetCreator(PDF_CREATOR);
  $pdf-&gt;SetAuthor('Asimegroup');
  $pdf-&gt;SetTitle('Asime Loan App');
  $pdf-&gt;SetSubject('Asime Loan Application Form');
  
  // set default monospaced font
  $pdf-&gt;SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  
  // set margins
  $pdf-&gt;SetMargins('5', '0', '0');
  
  
  // set auto page breaks
  $pdf-&gt;SetAutoPageBreak(TRUE, '0');
  
  // set image scale factor
  $pdf-&gt;setImageScale(PDF_IMAGE_SCALE_RATIO);
  
  // set some language-dependent strings (optional)
  if (@file_exists(dirname(__FILE__).'examples\lang\eng.php')) {
      require_once(dirname(__FILE__).'examples\lang\eng.php');
      $pdf-&gt;setLanguageArray($l);
  }
  
  // set font
  $pdf-&gt;SetFont('helvetica', '', 10);
  
  // add a page
  $pdf-&gt;AddPage();
  
  // Just including 2 rows here for example sake.
  
  $html = '
  &lt;body&gt;
  &lt;br/&gt;
  &lt;table&gt;
    &lt;tr&gt;
      &lt;td height="40px"&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td align="right" class="heading"  width="175px"&gt;Personal&lt;/td&gt;
      &lt;td align="left" class="heading" width="210px"&gt;Details:&lt;/td&gt;
      &lt;td width="10px"&gt;&lt;/td&gt;
      &lt;td align="right" class="heading" width="150px"&gt;Relative not&lt;/td&gt;
      &lt;td align="left" class="heading" width="170px"&gt;living with you:&lt;/td&gt;
      &lt;td width="10px"&gt;&lt;/td&gt;
      &lt;td align="right" class="heading" width="185px"&gt;Expenses:&lt;/td&gt;
      &lt;td width="100px"&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class="subheading"&gt;SA Citizen?:&lt;/td&gt;
      &lt;td class="data"&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td class="subheading"&gt;Name:&lt;/td&gt;
      &lt;td class="data"&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td class="subheading"&gt;Bond / Rent:&lt;/td&gt;
      &lt;td class="data"&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/table&gt;';
  
  
    // output the HTML content
    $pdf-&gt;writeHTML($html, true, false, true, false, '');
  
    // reset pointer to the last page
    $pdf-&gt;lastPage();
  
    // ---------------------------------------------------------
  
    //Close and output PDF document
    $pdf-&gt;Output('example_061.pdf', 'F');
  
  ?&gt;
  </code></pre>
  
  <p>PHP Database Loop:</p>
  
  <pre><code>&lt;?php
  // server info
  $server = 'localhost';
  $user = 'root';
  $pass = '';
  $db = 'debtdb';
  
  
  // connect to the database
  $conn = new mysqli($server, $user, $pass, $db);
  
  // show errors (remove this line if on a live site)
  mysqli_report(MYSQLI_REPORT_ERROR);
  
  if(!isset($_POST['agentname'])) {
  $search_sql="SELECT * FROM daily";
  $search_query=mysqli_query($conn, $search_sql);
  $search_rs=mysqli_fetch_assoc($search_query);
  }
  
   if(!empty($search_query))   {
  
    while($search_rs = mysqli_fetch_array($search_query)) {
  ?&gt;
  
  &lt;body&gt;
  
  // Just including 2 rows here for example sake.
  
  &lt;table&gt;
    &lt;tr&gt;
      &lt;td align="right" class="heading"  width="175px"&gt;Personal&lt;/td&gt;
      &lt;td align="left" class="heading" width="210px"&gt;Details:&lt;/td&gt;
      &lt;td width="10px"&gt;&lt;/td&gt;
      &lt;td align="right" class="heading" width="150px"&gt;Relative not&lt;/td&gt;
      &lt;td align="left" class="heading" width="170px"&gt;living with you:&lt;/td&gt;
      &lt;td width="10px"&gt;&lt;/td&gt;
      &lt;td align="right" class="heading" width="185px"&gt;Expenses:&lt;/td&gt;
      &lt;td width="100px"&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class="subheading"&gt;SA Citizen?:&lt;/td&gt;
      &lt;td class="data"&gt;&lt;?php echo $search_rs["per_citizen"]; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td class="subheading"&gt;Name:&lt;/td&gt;
      &lt;td class="data"&gt;&lt;?php echo $search_rs["rel_name"]; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td class="subheading"&gt;Bond / Rent:&lt;/td&gt;
      &lt;td class="data"&gt;R&amp;nbsp;&lt;?php echo $search_rs["exp_bondrent"]; ?&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/table&gt;
    &lt;?php
      }
    }
    ?&gt;
  </code></pre>
  
  <p>Each of these 2 examples works. The following is where I got stuck when I merge the 2:</p>
  
  <pre><code>&lt;?php
  // server info
  $server = 'localhost';
  $user = 'root';
  $pass = '';
  $db = 'debtdb';
  
  
  // connect to the database
  $conn = new mysqli($server, $user, $pass, $db);
  
  // show errors (remove this line if on a live site)
  mysqli_report(MYSQLI_REPORT_ERROR);
  
  if(!isset($_POST['agentname'])) {
  $search_sql="SELECT * FROM daily";
  $search_query=mysqli_query($conn, $search_sql);
  $search_rs=mysqli_fetch_assoc($search_query);
  }
  
   if(!empty($search_query))   {
  
    while($search_rs = mysqli_fetch_array($search_query)) {
  
  
  $pdf = new TCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false);
  
  // set document information
  $pdf-&gt;SetCreator(PDF_CREATOR);
  $pdf-&gt;SetAuthor('Asimegroup');
  $pdf-&gt;SetTitle('Asime Loan App');
  $pdf-&gt;SetSubject('Asime Loan Application Form');
  
  // set default monospaced font
  $pdf-&gt;SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  
  // set margins
  $pdf-&gt;SetMargins('5', '0', '0');
  
  
  // set auto page breaks
  $pdf-&gt;SetAutoPageBreak(TRUE, '0');
  
  // set image scale factor
  $pdf-&gt;setImageScale(PDF_IMAGE_SCALE_RATIO);
  
  // set some language-dependent strings (optional)
  if (@file_exists(dirname(__FILE__).'examples\lang\eng.php')) {
      require_once(dirname(__FILE__).'examples\lang\eng.php');
      $pdf-&gt;setLanguageArray($l);
  }
  
  // set font
  $pdf-&gt;SetFont('helvetica', '', 10);
  
  // add a page
  $pdf-&gt;AddPage();
  
  // Just including 2 rows here for example sake.
  
  $html = '
  &lt;table&gt;
    &lt;tr&gt;
      &lt;td align="right" class="heading"  width="175px"&gt;Personal&lt;/td&gt;
      &lt;td align="left" class="heading" width="210px"&gt;Details:&lt;/td&gt;
      &lt;td width="10px"&gt;&lt;/td&gt;
      &lt;td align="right" class="heading" width="150px"&gt;Relative not&lt;/td&gt;
      &lt;td align="left" class="heading" width="170px"&gt;living with you:&lt;/td&gt;
      &lt;td width="10px"&gt;&lt;/td&gt;
      &lt;td align="right" class="heading" width="185px"&gt;Expenses:&lt;/td&gt;
      &lt;td width="100px"&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class="subheading"&gt;SA Citizen?:&lt;/td&gt;
      &lt;td class="data"&gt;&lt;?php echo $search_rs["per_citizen"]; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td class="subheading"&gt;Name:&lt;/td&gt;
      &lt;td class="data"&gt;&lt;?php echo $search_rs["rel_name"]; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td class="subheading"&gt;Bond / Rent:&lt;/td&gt;
      &lt;td class="data"&gt;R&amp;nbsp;&lt;?php echo $search_rs["exp_bondrent"]; ?&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/table&gt;';
  
    // output the HTML content
    $pdf-&gt;writeHTML($html, true, false, true, false, '');
  
    // reset pointer to the last page
    $pdf-&gt;lastPage();
  
    // ---------------------------------------------------------
  
    //Close and output PDF document
    $pdf-&gt;Output('&lt;?php echo $search_rs["agentname"]; ?&gt; - &lt;?php echo $search_rs["dateofsale"]; ?&gt;.pdf', 'F');
  
      }
    }
    ?&gt;
  </code></pre>
  
  <p>Gives me :</p>
  
  <p>The localhost page isn’t working</p>
  
  <p>localhost is currently unable to handle this request.
  HTTP ERROR 500</p>