I want to make a pdf file in php. I write code like this:

<?php
require_once __DIR__ . '/vendor/autoload.php';
include('conn.php');
$res = mysqli_query($conn, "select * from smash");

if (mysqli_num_rows($res) > 0) {
    $html = '<table>';
    $html = '<tr><td>ID</td><td>Name</td><td>FatherName</td><td>Address</td><td>Phone</td><td>Class</td><td>Qualification</td><td>Branch</td><td>rollno</td></tr>';
    while ($row = mysqli_fetch_assoc($res)) {
        $html .= '<tr><td>' . $row['id'] . '</td><td>' . $row['name'] . '</td><td>' . $row['fname'] . '</td>
        <td>' . $row['address'] . '</td><td>' . $row['phone'] . '</td><td>' . $row['class'] . '</td>
        <td>' . $row['qualification'] . '</td><td>' . $row['branch'] . '</td>
        <td>' . $row['rollno'] . '</td><td>' . $row['gender'] . '</td>
        <td>' . $row['birth'] . '</td></tr>'
    }
    $html= '</table>';
  {
    $html = "Data not found"
  }};

$mpdf = new mPDF();
$mpdf->WriteHTML($html);
$file = time() . '.pdf';
$mpdf->output($file, 'D');

But a error show: Parse error: syntax error, unexpected token "}" in C:\xampp\htdocs\Management\printstudentdata.php on line 15

I use PHp 8.

Dani AI

Generated

A PHP parse error like "unexpected token }" means the parser found a closing brace where it was still expecting the end of a statement or the continuation of an expression. Common root causes are a missing semicolon, an unterminated string or parenthesis, or unbalanced braces — especially likely when concatenating long HTML strings across multiple lines. The error message points to where PHP noticed the problem, not always where it began, so check a few lines back from the reported line.

Practical checks that find these issues fast:

  • Run the built-in linter: php -l path/to/file.php to get a precise syntax complaint.
  • Turn on full error reporting during debugging: ini_set('display_errors',1); error_reporting(E_ALL);.
  • Use an editor/IDE with bracket matching and syntax highlighting to spot unclosed quotes/parentheses.
  • Search for missing semicolons at the end of long concatenation lines; prefer .= when appending instead of reassigning with =. For large HTML, use a heredoc/nowdoc or a separate template file and capture it with output buffering to avoid quote juggling.

Credit to for the brace check and for the formatter idea — both are exactly the right first steps. ’s follow-up shows the same class of fixes work in practice. After resolving syntax errors, add a couple of robustness improvements: check query results before calling mysqli_num_rows, handle the case when the query fails, and avoid building SQL with raw user input (use prepared statements). Also rely on Composer/autoload and the library's namespaced constructor rather than guessing class names.

Quick checklist before re-running PDF generation:

  • lint the file (php -l),
  • enable errors,
  • confirm every statement ends with ;,
  • ensure if { ... } else { ... } pairs match,
  • consider switching multi-line HTML to a template/heredoc to reduce future syntax mistakes.

Recommended Answers

All 3 Replies

I think I see 4 or more problems with this code.

  1. Unbalanced braces.
  2. No matter what happens prior to line 18, $html sets the string unconditionally to data not found.
  3. I see on line 7 you building the string but line 8 negates line 7.
  4. Also, line 16 sets $html to well, what you wrote there.

Check your braces and consider moving line 18 to line 5.

I make some change in this code. because I did some mistake. now it works:

<?php
require_once __DIR__ . '/vendor/autoload.php';
include('conn.php');
$res = mysqli_query($conn, "select * from smash");

if (mysqli_num_rows($res) > 0) {
  $html = '<table>';
  $html .= '<tr><td>ID</td><td>Name</td><td>FatherName</td><td>Address</td><td>Phone</td><td>Class</td><td>Qualification</td><td>Branch</td><td>rollno</td></tr>';
  while ($row = mysqli_fetch_assoc($res)) {
    $html .= '<tr><td>' . $row['id'] . '</td><td>' . $row['name'] . '</td><td>' . $row['fname'] . '</td>
        <td>' . $row['address'] . '</td><td>' . $row['phone'] . '</td><td>' . $row['class'] . '</td>
        <td>' . $row['qualification'] . '</td><td>' . $row['branch'] . '</td>
        <td>' . $row['rollno'] . '</td><td>' . $row['gender'] . '</td>
        <td>' . $row['birth'] . '</td></tr>';
  }
  $html .= '</table>';
} else {
  $html = "Data not found";
};

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$file = time() . '.pdf';
$mpdf->output($file, 'D');

Hi. Please check that you have enclosed all braces in your code. Best option is to use an online beautifier or formatter and check if all is aligned. Close code section that is not entire closed, check line 6 and 10 and see if they have matching closed bracket.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.