1

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...

9
  • 1
    I don't see any call of array_push() but I would simply use $your_array[] = 'some value'; instead. Commented Sep 3, 2014 at 19:07
  • Show tcpdf.php line 22165 where you use array_push(), and where you declare the array you use in array_push() Commented Sep 3, 2014 at 19:10
  • Did you note this note about writeHTML: NOTE: all the HTML attributes must be enclosed in double-quote. Commented Sep 3, 2014 at 19:15
  • 1
    This looks terrifyingly insecure. Are you sure your user parameters are properly escaped? mysql_query is an obsolete interface and should not be used in new applications and will be removed in future versions of PHP. A modern replacement like PDO is not hard to learn. If you're new to PHP, a guide like PHP The Right Way can help explain best practices. Commented Sep 3, 2014 at 19:16
  • 1
    You've got to concatenate your HTML table in just one single string. That's the way this class works. I've ever managed to concatenate such strings myself if needed. Instead of outputting it you add it to your string. Commented Sep 3, 2014 at 20:07

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response i had got the solution using concat... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.