1

I want to retrieve data from custom table, that I've made inside the wordpress database and display it in a wordpress page , like posts

Thanks in advance

6

Here is an example code that will get the data and then display it:

global $wpdb;
// this adds the prefix which is set by the user upon instillation of wordpress
$table_name = $wpdb->prefix . "your_table_name";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
?>
<ul>
foreach ($retrieve_data as $retrieved_data){ ?>
<li><?php echo $retrieved_data->column_name;?></li>
<li><?php echo $retrieved_data->another_column_name;?></li>
<li><?php echo $retrieved_data->as_many_columns_as_you_have;?></li>
<?php 
}
?>
</ul>
<?php

It's good practice to use unique names for variables and functions, so you may want to add a unique prefix to all your variables or functions IE: ($prefix_table_name where "prefix" would be something unique such as the abbreviation of your theme or plugin.)

Reference - wpdb - codex

1

Please try this code for display all the records from database in wordpress. For this firstly need to create a file.php inside your selected wordpress folder and then use this file as a template. And this code will work perfectly Thank you all.

<?php /* Template Name: your template name */ ?>
<?php get_header(); ?>
<table border="1">
    <tr>
     <th>ID</th>
     <th>FULL NAME</th>
     <th>BRANCH NAME</th>
     <th>E-MAIL ID</th>
     <th>Mobile Number</th>
     <th>Course</th>
     <th>Address</th>
     <th>City</th>
     <th>Zip Code</th>
    </tr>

      <?php

        global $wpdb;
        $result = $wpdb->get_results( "SELECT * FROM wp_example");
        foreach ( $result as $print )   { ?>
          <tr>
                  <td>  <?php echo $print->id; ?> </td>
                  <td><?php echo $print->firstname; ?> </td>
                  <td> <?php echo $print->branch ; ?> </td>
                  <td> <?php echo $print->email; ?> </td>
                  <td><?php echo $print->mobile; ?> </td>
                  <td> <?php echo $print->course; ?> </td>
                  <td> <?php echo $print->address; ?> </td>
                  <td><?php echo $print->city; ?> </td>
                  <td> <?php echo $print->zip ; ?> </td>
          </tr>
            <?php }
      ?>

</table>
<?php get_header(); ?>
  • 1
    That does not answer the question. Please re-read it. – kaiser Jan 18 '17 at 12:09
0

Sounds like you're looking for $wpdb. You'll need to write all your own functions and such. I strongly recommend sticking to the established naming conventions (stuff like the_blah and get_blah, maybe with a prefix) for ease of readability and for consistency.

0

Modification of @Kirill Fuchs's answer. If you use this code on shortcode this may create a problem. It may display the outputs in wrong order. To avoid the I used return instead of echo. Try to do this on shortcode this way:

add_shortcode('custom_db', function(){
global $wpdb;
$table_name = $wpdb->prefix . 'liveshoutbox';
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
foreach ($retrieve_data as $retrieved_data){ 
    $f_name = $retrieved_data->column_name;
    $f_text = $retrieved_data->another_column_name;
}
    $output = '<div class="wrap">
                    <h2>Table of clients.</h2>
                    <table>
                      <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Email</th>
                      </tr> 
                      <tr>
                        <td>'. $f_name .'</td>
                        <td>'. $f_text .'</td>
                      </tr>
                    </table>            
                </div>';
    return $output;
} );
  • You can also use output buffer to collect the html for return. Use ob_start(); at the beginning and return ob_get_clean(); – Bikash Waiba May 4 '18 at 5:59
  • No need to write many lines in single quote. – Bikash Waiba May 4 '18 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.