How to run mysql query inside a WordPress post with shortcode without sending it to the footer

I have a mysql code which works perfectly. I have gone ahead to create a custom shortcode for it so that I can embed the contents inside a post. I am having problems with the shortcode executing below the footer. I initially thought it’s a plugin/theme issue but it’s clearly not. Seems there is something with the code that makes it go below the footer. I think so because I added some text and images in the post content and everything runs properly. Here’s the code that generates the shortcode [todayrecords]

<?php function today_records_shortcode() {     ob_start();     ?> <table border="1"> <th colspan="5" style="text-align:center;"> <h2 class="tomorrow">TODAY RECORDS </h2> </th> <tr> <th>Day</th>  <th>Date</th>  <th>Exam Type</th>  <th>Student Name</th> </tr>   <?php     global $wpdb;     $result = $wpdb->get_results (  "SELECT * FROM wp_mytyt WHERE date=DATE(NOW()) UNION ALL SELECT * FROM wp_myyg WHERE date=DATE(NOW()) ORDER BY date, examtype ASC;" );     foreach ( $result as $print )   {     ?>     <tr> <td><?php echo $print->day;?></td> <td><?php echo $print->date;?></td> <td><?php echo $print->examtype;?></td> <td><?php echo $print->sudentname;?></td>     </tr>         <?php }   ?>        <?php     return ob_get_clean(); }   add_shortcode('todayrecords', 'today_records_shortcode'); 

Why is this code output going below the footer?

Add Comment
1 Answer(s)

Can you try this?

function today_records_shortcode() {     ob_start();     ?>     // Your html code or output.     <?php     $var = ob_get_contents();     ob_end_clean();     echo $var; }  add_shortcode('todayrecords', 'today_records_shortcode'); 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.