Jump to content

Selecting Data from MySQL to display in TCPDF


Recommended Posts

Hi all,

I am trying to get data from MySQL to display in a html table in TCPDF but it is only displaying the ID.

$accom = '<h3>Accommodation:</h3>
<table cellpadding="1" cellspacing="1" border="1" style="text-align:center;">

$id = $_GET['id'];
$location = $row['location'];
$sql = "SELECT * FROM tours WHERE id = $id";
$result = $con->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {


Anyone got any ideas?

Link to comment
Share on other sites

you cannot concatenate general php logic together with a string. you can only concatenate php code that evaluates to a string. the reason it's displaying the id is because you are concatenating the result of the $id = $_GET['id']; assignment statement onto the end of the string in $accom. that's the end of the concatenation in the existing code. the rest of the strings being built in the php code are being discarded, because they are not 'attached' to anything. you would need to use $accom .= to add the string that's being built inside of the while(){} loop and again for the string that's being built after the end of the conditional logic.

edit: you would also need to terminate the string assignment statement, right before the (needless) $id = $_GET['id']; assignment statement, with a ; rather than with a concatenation dot. you should also use a prepared query when supplying external, unknown, dynamic values to a query when it gets executed, instead of putting values directly into the sql query statement. 


Edited by mac_gyver
Link to comment
Share on other sites

Thanks both.

Sorry it took so long to respond. Been working on the road.

This seems to work:

// add a page

$html4 = '<h2>Contacts</h2>
<table width="600px" border="1px">';

//data iteration

$sql = "SELECT * FROM tours";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
    // concatenate a string, instead of calling $pdf->writeHTML()
    $html4 .= '<tr><td>'.$location.'</td></tr>';
$html4 .= '</table>';

// output the HTML content

$pdf->writeHTML($html4, true, false, true, false, '');

Hope this is what you guys meant?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.