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.

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.