Title: SQL Results into HTML Table
Last modified: August 22, 2016

---

# SQL Results into HTML Table

 *  Resolved [cragga_lfc](https://wordpress.org/support/users/cragga_lfc/)
 * (@cragga_lfc)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/)
 * Hi i am new to this, however, i feel i am learning and getting on with it. however
   i have ran into a problem.
 * the insert php i have got working on this page but the results are not great 
   in terms of they need formatting
    [http://luxor-lettings.com/our-properties/click-through/](http://luxor-lettings.com/our-properties/click-through/)
 * but i want to get the results into a HTML table so i have attempted it on another
   page – i have attempted with the following code on this page:
    [http://luxor-lettings.com/results-2/](http://luxor-lettings.com/results-2/)
 * <html>
    <head> <title>Available Properties</title> </head> <body> <table> <thead
   > <tr> <td>Property Image</td> <td>Property Description</td> <td>Property Availability
   </td> </tr> </thead> <tbody> [insert_php] $servername = “db567706584.db.1and1.
   com”; $username = “username”; $password = “password”; $dbname = “db567706584”;//
   Create connection $conn = new mysqli($servername, $username, $password, $dbname);//
   Check connection if ($conn->connect_error) { die(“Connection failed: ” . $conn-
   >connect_error); } $sql = “SELECT `Property Thumbnail URL`,`Property Title4HTML`,`
   Image Tag` FROM `Properties`,`Available` WHERE `Property Available Flag` = `Flag`
   AND `Property Available Flag` = ‘2’”; $result = $conn->query($sql); if ($result-
   >num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()){[/
   insert_php] <tr> <td>[insert_php] echo $row[‘Property Image’][/insert_php]</td
   > <td>[insert_php] echo $row[‘Property Description’][/insert_php]</td> <td>[insert_php]
   echo $row[‘Property Availability’][/insert_php]</td> </tr> [insert_php] } } else{
   echo “0 results”; } $conn->close(); [/insert_php] </tbody> </table> </body> </
   html>
 * but i get get errors – any help appreciated
 * what should i be changing to get the results into a table? thanks
 * [https://wordpress.org/plugins/insert-php/](https://wordpress.org/plugins/insert-php/)

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [WillBontrager](https://wordpress.org/support/users/willbontrager/)
 * (@willbontrager)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/#post-5902888)
 * Hi cragga_lfc,
 * Insert PHP is a closed system. Everything that it needs to work with must be 
   in the code between the [insert_php] and [/insert_php] tags. See
 * [http://www.willmaster.com/software/WPplugins/insert-php-wordpress-plugin-instructions.php#inandofitself](http://www.willmaster.com/software/WPplugins/insert-php-wordpress-plugin-instructions.php#inandofitself)
 * (Other information on that page may be useful.)
 * As an example, this section of your code
 *     ```
       while($row = $result->fetch_assoc()) {
       [/insert_php]
       <tr>
       <td>[insert_php] echo $row['Property Image'][/insert_php]</td>
       <td>[insert_php] echo $row['Property Description'][/insert_php]</td>
       <td>[insert_php] echo $row['Property Availability'][/insert_php]</td>
       </tr>
       [insert_php]
       }
       } else {
       echo "0 results";
       }
       $conn->close();
       [/insert_php]
       ```
   
 * doesn’t work because the second Insert PHP block doesn’t know what $row is. And
   the while loop is broken up.
 * For that particular section, this might work (untested, presented only as an 
   example):
 *     ```
       while($row = $result->fetch_assoc()) {
       echo '<tr>';
       echo '<td>';
       echo $row['Property Image'];
       echo '</td>';
       echo '<td>';
       echo $row['Property Description']
       echo '</td>';
       echo '<td>';
       echo $row['Property Availability']
       echo '</td>';
       echo '</tr>';
       }
       } else {
       echo "0 results";
       }
       $conn->close();
       [/insert_php]
       ```
   
 * Will
 *  Thread Starter [cragga_lfc](https://wordpress.org/support/users/cragga_lfc/)
 * (@cragga_lfc)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/#post-5902890)
 * Hi Will
 * thank you so much for helping me out i really appreciate it as i have been pulling
   my hair out for the last 3 days.
 * it has made changes to the errors now but no results still appear.
 * the error message i get now is:
 * _Parse error: syntax error, unexpected ‘echo’ (T\_ECHO), expecting ‘,’ or ‘;’
   in /homepages/46/d567671028/htdocs/clickandbuilds/WordPress/LuxorEstates/wp-content/
   plugins/insert-php/insert\_php.php(48) : eval()’d code on line 23_
 * the displayed results are on:
    [http://luxor-lettings.com/results-2/](http://luxor-lettings.com/results-2/)
 * any help on this would be great. thank you
 *  [WillBontrager](https://wordpress.org/support/users/willbontrager/)
 * (@willbontrager)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/#post-5902895)
 * There’s an error on line 23 of the code between the [insert_php] and [/insert_php]
   tags.
 * See this page for more information about error messages:
 * [http://www.willmaster.com/software/WPplugins/insert-php-wordpress-plugin-instructions.php#errormessages](http://www.willmaster.com/software/WPplugins/insert-php-wordpress-plugin-instructions.php#errormessages)
 * And do read the rest of the page. It’s not an idle suggestion. It will help you.
 * Will
 *  Thread Starter [cragga_lfc](https://wordpress.org/support/users/cragga_lfc/)
 * (@cragga_lfc)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/#post-5902901)
 * thank you Will
 * i have read the whole page and i think i have fixed the problem – however no 
   parse issues appear
 * i get 0 results returned so it makes me think the script is wrong, but when i
   copy the script in phpMyAdmin it works
 * i dont know where to turn to now
 * the page – [http://luxor-lettings.com/results-2/](http://luxor-lettings.com/results-2/)
 * here is my code:
    <html> <head> <title>Available Properties</title> </head> <
   body> <table> <thead> <tr> <td>Property Image</td> <td>Property Description</
   td> <td>Property Availability</td> </tr> </thead> <tbody> [insert_php] $servername
   = “db567706584.db.1and1.com”; $username = “username”; $password = “password”;
   $dbname = “db567706584”; // Create connection $conn = new mysqli($servername,
   $username, $password, $dbname); // Check connection if ($conn->connect_error){
   die(“Connection failed: ” . $conn->connect_error); } $sql = “SELECT Property 
   Thumbnail URL,Property Title4HTML,Image Tag FROM Properties,Available WHERE Property
   Available Flag = Flag AND Property Available Flag = ‘2’”; $result = $conn->query(
   $sql); if ($result->num_rows > 0) { // output data of each row while($row = $
   result->fetch_assoc()) { echo ‘<tr>’; echo ‘<td>’; echo $row[‘Property Image’];
   echo ‘</td>’; echo ‘<td>’; echo $row[‘Property Description’]; echo ‘</td>’; echo‘
   <td>’; echo $row[‘Property Availability’]; echo ‘</td>’; echo ‘</tr>’; } } else{
   echo “0 results”; } $conn->close(); [/insert_php] </tbody> </table> </body> </
   html>
 * any help Will would be greatly appreciated as i truly am thankful for your help
   so far as i feel i am so close
 * thank you
 *  Thread Starter [cragga_lfc](https://wordpress.org/support/users/cragga_lfc/)
 * (@cragga_lfc)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/#post-5902902)
 * Hi Will
 * i fixed the 0 results as my SQL script was wrong – i amended it to the following
   which means the “0 results” no longer appears
 * SELECT `Property Thumbnail URL`,`Property Title4HTML`,`Image Tag` FROM `Properties`,`
   Available` WHERE `Property Available Flag` = `Flag` AND `Property Available Flag`
   = ‘2’
 * HOWEVER, i get no results showing now in a table
 * do you have any ideas?
 * thanks Craig
 *  Thread Starter [cragga_lfc](https://wordpress.org/support/users/cragga_lfc/)
 * (@cragga_lfc)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/#post-5902903)
 * apologies the fields and tables are not showing with the “`” i added at the beginning
   and the end of each field
 *  Thread Starter [cragga_lfc](https://wordpress.org/support/users/cragga_lfc/)
 * (@cragga_lfc)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/#post-5902920)
 * tried something different from my search on google:
 * // output data of each row
    while($row = $result->fetch_assoc()) { echo “<tr>
   <td>” . $row[‘Property Image’] . “</td><td>” . $row[‘Property Description’] .“
   </td><td>” . $row[‘Property Availability’] . “</td></tr>”; } } else { echo “0
   results”; } $conn->close();
 * – but still no results show
 *  Thread Starter [cragga_lfc](https://wordpress.org/support/users/cragga_lfc/)
 * (@cragga_lfc)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/#post-5902921)
 * Hi Will i did it!!!
 * I got data back into a table
 * i was “echoing” the field titles and not the fields from the db – dhuur!!
 * so i amend to:
 * while($row = $result->fetch_assoc()) {
    echo “<tr><td>” . $row[‘Property Thumbnail
   URL’] . “</td><td>” . $row[‘Property Title4HTML’] . “</td><td>” . $row[‘Image
   Tag’] . “</td></tr>”; }
 * now the results appear – but they don’t seem to be in a nice format – the text
   is aligned at the bottom
 * do you have any ideas how i can format this table now to look nice?!
 * thank you for your help was great to resolve it – just the formatting i can’t
   seem to figure out.
 * any ideas? would be greatly appreciated
 *  Thread Starter [cragga_lfc](https://wordpress.org/support/users/cragga_lfc/)
 * (@cragga_lfc)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/#post-5902925)
 * i think i have resolved it
 * i needed to add
 * td {
    vertical-align: top; }
 * to the custom CSS plugin and the text when to the top
 * by all means tell me if this is wrong and wont work on other pages but it seems
   to have worked
 * beginners look if so
 * this is where i got the info:
    [http://stackoverflow.com/questions/2659508/html-align-table-rows-on-top](http://stackoverflow.com/questions/2659508/html-align-table-rows-on-top)
 *  Thread Starter [cragga_lfc](https://wordpress.org/support/users/cragga_lfc/)
 * (@cragga_lfc)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/#post-5902938)
 * i think this is resolved – seems to have worked a treat on my pages adding the
   CSS
 *  [WillBontrager](https://wordpress.org/support/users/willbontrager/)
 * (@willbontrager)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/#post-5902939)
 * Thanks for noting your progress as you went along. And I’m glad it’s working 
   for you.
 * Will

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘SQL Results into HTML Table’ is closed to new replies.

 * ![](https://ps.w.org/insert-php/assets/icon-256x256.gif?rev=3523853)
 * [Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts](https://wordpress.org/plugins/insert-php/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/insert-php/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/insert-php/)
 * [Active Topics](https://wordpress.org/support/plugin/insert-php/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/insert-php/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/insert-php/reviews/)

 * 11 replies
 * 2 participants
 * Last reply from: [WillBontrager](https://wordpress.org/support/users/willbontrager/)
 * Last activity: [11 years, 2 months ago](https://wordpress.org/support/topic/sql-results-into-html-table/#post-5902939)
 * Status: resolved