Moorcam Posted March 6, 2021 Share Posted March 6, 2021 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;"> <tr> <th><strong>Organisation</strong></th> <th><strong>Contact</strong></th> <th><strong>Phone</strong></th> </tr> <tbody> <tr>'. $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()) { '<td>'.$location.'</td> <td>David</td> <td>0412345678</td> </tbody>'; } } '</tr> </table>'; Anyone got any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/312253-selecting-data-from-mysql-to-display-in-tcpdf/ Share on other sites More sharing options...
Barand Posted March 6, 2021 Share Posted March 6, 2021 I'm surprised it's even displaying the id. You are setting $location = $row['location'] before you have retrieved any rows from the query. Inside the while loop you don't do anything, just putting a string inside some quotes. Quote Link to comment https://forums.phpfreaks.com/topic/312253-selecting-data-from-mysql-to-display-in-tcpdf/#findComment-1584935 Share on other sites More sharing options...
mac_gyver Posted March 6, 2021 Share Posted March 6, 2021 (edited) 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 March 6, 2021 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/312253-selecting-data-from-mysql-to-display-in-tcpdf/#findComment-1584936 Share on other sites More sharing options...
Moorcam Posted March 9, 2021 Author Share Posted March 9, 2021 Thanks both. Sorry it took so long to respond. Been working on the road. This seems to work: // add a page $pdf->AddPage(); $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()) { { $location=$row['location']; // 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? Quote Link to comment https://forums.phpfreaks.com/topic/312253-selecting-data-from-mysql-to-display-in-tcpdf/#findComment-1584962 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.