• Resolved Charlie

    (@cdarling)


    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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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.

    Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter Charlie

    (@cdarling)

    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!!

    Thread Starter Charlie

    (@cdarling)

    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>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Tiny test harness for SQL statements’ is closed to new replies.