query mysql in fpdi file

I have the below file that opens a pdf and puts some text at the top. This is working fine. But what i want to do now is replace mypdf.pdf with the results of a mysql query. Similar to this:
$pageCount = $pdf->setSourceFile('../folder/folder/$mypdf');
and
$pdf->Write(0, '$sampletext');

Each person going to this page could receive a different pdf, hence the need to query the database to get the pdf they need.
Here is my code page:

<?php
//  brings the id from previous page to use in database query
$id = $_GET['id'];
// trying to query database errors the page


use setasign\Fpdi\Fpdi;
require_once 'fpdi/src/autoload.php';
require_once('fpdf/fpdf.php');

// initiate FPDI
$pdf = new Fpdi();

// get the page count
$pageCount = $pdf->setSourceFile('../folder/folder/mypdf.pdf');
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
    // import a page
    $templateId = $pdf->importPage($pageNo);

    $pdf->AddPage();
    // use the imported page and adjust the page size
    $pdf->useTemplate($templateId, ['adjustPageSize' => true]);

    // now write some text above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 10);
$pdf->Write(0, 'This is just a simple text');
}

// Output the new PDF
$pdf->Output();
?>

How can i query the database and put in my variables without erroring the page?

I hope this makes sense. If someone can point me in the right direction it would be appreciate.

Thanks.

mysqli query skipping row 0

I am using bootstrap modals, when i query to populate a dropdown to fill in to my form, my query is show all but the first record and i can't figure out why.

My form looks like this

<div class="form-group">
            <label for="deptcode">Department</label>
                <select class="form-control" id="deptcode" name="deptcode" value="<?php echo $mem['deptcode'];?>"
                    <?php
                    $departments = $mysqli->query("Select * FROM department ORDER BY deptcode");
                     while ($dept = mysqli_fetch_assoc($departments)){

                       $tempdeptname = $dept['deptname'];
                       $tempdeptcode = $dept['deptcode'];
                       //see what the current name is and display it first
                       if ($tempdeptcode == $mem['deptcode'])
                         echo "<option value='$tempdeptcode' selected='selected'>$tempdeptcode ($tempdeptname) </option>\n";
                       else
                       //if blank display first item in dropdown list
                       echo "<option value=\"$tempdeptcode\">$tempdeptcode ($tempdeptname) </option>\n";
                        }
                    }
                    ?>
                    endwhile;
                </select>
        </div>

This works perfectly other than not showing row 0.

Thanks in advance.