Forums

What does it take to connect to the database? (3 posts)

  1. mangeletti
    Member
    Posted 4 years ago #

    I'm creating a WordPress plugin and would like to know what it takes to access the database?

    I used the following two lines:

    include_once("{$_SERVER['DOCUMENT_ROOT']}/wp-config.php");
    include_once("{$_SERVER['DOCUMENT_ROOT']}/wp-includes/wp-db.php");

    Do I need them both? It seems like some plugins that access the db don't use the above two lines?

    In addition, what does the below line mean? What is ABSPATH and WPINC?

    include_once(ABSPATH . WPINC . '/rss.php');

  2. Michael Torbert
    WordPress Virtuoso
    Posted 4 years ago #

    You don't need any of that. You just need to use the WordPress database class. Have you tried Google?

    http://codex.wordpress.org/wpdb was the first result of many that would be helpful.

    As for ABSPATH and WPINC, Google would help with those also.
    ABSPATH = absolute path
    WPINC = WordPress includes (read:wp-includes)

  3. mangeletti
    Member
    Posted 4 years ago #

    ok, so if you don't include:
    include_once("{$_SERVER['DOCUMENT_ROOT']}/wp-config.php");
    include_once("{$_SERVER['DOCUMENT_ROOT']}/wp-includes/wp-db.php");

    then your SQL query changes a bit:

    You have to put $wpdb-> before everything now AND it looks like you drop wp_ from the table and field names? Is the below code right?

    $sql = 'SELECT $wpdb->wp_posts.post_title, $wpdb->wp_posts.guid, $wpdb->wp_comments.comment_author_url'
    . ' FROM $wpdb->wp_posts'
    . ' LEFT JOIN $wpdb->wp_comments ON $wpdb->wp_posts.id = $wpdb->wp_comments.comment_post_ID'
    . ' WHERE $wpdb->wp_comments.comment_author_email = ('
    . ' SELECT $wpdb->wp_comments.comment_author_email'
    . ' FROM $wpdb->wp_comments'
    . ' WHERE $wpdb->wp_comments.comment_ID = '.$nCommentID
    . ' AND $wpdb->wp_comments.comment_type != \'pingback\' ) '
    . ' ORDER BY $wpdb->wp_comments.comment_date DESC'
    . ' LIMIT 0 , 3';

Topic Closed

This topic has been closed to new replies.

About this Topic