WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Tiny test harness for SQL statements (5 posts)

  1. Charlie
    Member
    Posted 1 year ago #

    What's the smallest php file you can come up with that can be used for really quick-and-dirty testing of SQL Select statements on the WP database?

    I'm new to this environment and just want a way to quickly see what this or that simple SQL Select will do.

    Thanks!

  2. catacaustic
    Member
    Posted 1 year ago #

    I wouldn't use a PHP file on my server. I'd use the copy of phpMyAdmin that's set up on my hosting account. If you don't have that you could set up a small page that has a DB connection and a form and will run any SQL that's in the form when you submit it. Just be sure that if you do that, that you remove that as soon as you are finished with it - you don't want anyone else accidentally finding it and having direct access to your DB.

  3. bcworkz
    Member
    Posted 1 year ago #

    Just for fun, taking your question at face value. This is far from efficient, nor convenient, but it is quick and dirty, the file itself is quite short, and it's in keeping with the WP topics here :)

    <?php
    require_once('wp-load.php');
    print_r($wpdb->get_results(
    "Insert your test query here and upload before loading page"
    ));

    Seriously though, +1 for using phpMyAdmin.

  4. Charlie
    Member
    Posted 1 year ago #

    Wonderful! Quick and easy!!

    And it lets me test my SQL in a WP context, rather than just a purely SQL context (ie "SELECT ... FROM $wpdb->posts..." rather than "SELECT ... FROM wp_posts...").

    There's a time and a place for each, of course. And now I've got a way to do each.

    Thanks, bcworkz!!

  5. Charlie
    Member
    Posted 1 year ago #

    Here it is again, with a slight tweak to improve readability of the result, and with a sample SQL statement in place.

    <pre>
    <?php
    require_once('wp-load.php');
    print_r($wpdb->get_results(
    "SELECT $wpdb->posts.post_title, $wpdb->posts.ID,
            $wpdb->posts.post_date
       FROM $wpdb->posts
       WHERE $wpdb->posts.post_type = 'post'
         AND $wpdb->posts.post_status = 'publish'
       ORDER BY $wpdb->posts.post_date DESC"
    ));
    ?>
    </pre>

Topic Closed

This topic has been closed to new replies.

About this Topic