Forums

Database access: wpdb vs. native PHP (2 posts)

  1. rowan.bradley
    Member
    Posted 2 years ago #

    In my WordPress site I need to access some new tables that I've added to the WordPress MySQL database. I've been trying to use the wpdb functions, but I'm beginning to wonder why it's better to use wpdb than just to use the native PHP functions for carrying out MySQL queries. I'm wondering why the wpdb functions were written, and whether they are just as complex and difficult to use as the PHP native functions, as well as possibly less powerful.
    Can you tell me:
    - why should I use wpdb functions rather than native PHP MySQL functions?
    - will I get into trouble using native functions? What sort of trouble?
    - is there anything I can do with wpdb that I can't do with native functions?
    - any other arguments for or against.

    Many thanks - Rowan

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    If your table is called wp_mydata, then this would count the records in that database using wpdb.

    <?php
    $mydata_count = $wpdb->get_var("SELECT COUNT(*) FROM wp_mydata");
    ?>

    If you use wpdb you don't need to worry about any connect process. You can also use the prepare syntax as described in wpdb which is a safer mode...

    Another discussion:
    http://wordpress.org/support/topic/307504?replies=14#post-1199337

Topic Closed

This topic has been closed to new replies.

About this Topic