简体   繁体   中英

TCPDF MYSQL data retrieval and print in html table

i use tcpdf to generate pdf in my site...

i use the code to retrieve data from mysql and display it in a table in the pdf file...

$pdf->writeHTML('<table width="600px" border="1px">');

    //data iteration
        include('../connect.php');
         $rer = mysql_query("SELECT * FROM complaint WHERE DATE_FORMAT(date_time,'%Y-%m-%d') between '$rep_from' and '$rep_to' order by id DESC;",$con);

    while($rr=mysql_fetch_array($rer))
    {

    $id=$rr['id'];
    $c_id=$rr['ref_id'];
    $pdf->writeHTML('<tr><td>'.$c_id.'</td></tr>');
    }

    $pdf->writeHTML('<table>');

The problem is the code and iteration works fine... but when i use table tags, i shows

Warning: array_push() expects parameter 1 to be array, null given in tcpdf.php on line 22165

This line in the file has some thing to do with tables... what is wrong with my code???

Thanks in advance...

The documentation states that the HTML should be well formatted, I understand this that it should be well formed, complete elements from the opening tag until the closing tag. My experiments showed similar errors, if I didn't do that.

So you could rewrite this sample code to:

$html = '<table width="600px" border="1px">';

//data iteration
include('../connect.php');
$rer = mysql_query("SELECT * FROM complaint WHERE DATE_FORMAT(date_time,'%Y-%m-%d') between '$rep_from' and '$rep_to' order by id DESC;",$con);

while($rr=mysql_fetch_array($rer))
{
    $id=$rr['id'];
    $c_id=$rr['ref_id'];
    // concatenate a string, instead of calling $pdf->writeHTML()
    $html .= '<tr><td>'.$c_id.'</td></tr>';
}

$html .= '</table>';
$pdf->writeHTML($html);

You should see how the complete output gets collected in a string variable. Only the well formed complete table is given to $pdf->writeHTML() . That should work for you too.

Note:

This code should never see production. Use PDO or mysqli with parameterized prepared statements instead as tadman said in his comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM