Good solution for displaying a SQL query on a WordPress site

I have a wordpress site that I have uploaded a few custom SQL tables via phpMyAdmin. I’m trying to find a good way of displaying custom queries on my wordpress page. I’ve tried using WP_Datatables, but have run into too many problems with it.

Related posts

Leave a Reply

1 comment

  1. There are some answers to similar questions already on SO; it’s a good idea to search first, i.e. Display data from database inside <table> using wordpress $wpdb

    But basically use the WordPress database abstraction layer wpdb

    See https://developer.wordpress.org/reference/classes/wpdb/ for how to use it and see examples, such as below:

    // 1st Method - Declaring $wpdb as global and using it to
      execute an SQL query statement that returns a PHP object
    
    global $wpdb;
    $results = $wpdb->get_results( 'SELECT * FROM wp_options WHERE option_id = 1', OBJECT );
    
    // 2nd Method - Utilizing the $GLOBALS superglobal.
    Does not require global keyword ( but may not be best practice )
    
    $results = $GLOBALS['wpdb']->get_results( 'SELECT * FROM wp_options WHERE option_id = 1', OBJECT );
    

    The $wpdb object is not limited to the default tables created by
    WordPress; it can be used to read data from any table in the WordPress
    database (such as custom plugin tables). For example to SELECT some
    information from a custom table called “mytable”, you can do the
    following.

    $myrows = $wpdb->get_results( "SELECT id, name FROM mytable" );