• how do I use the wpdb class within a plugin?

    I included the class…
    include_once(ABSPATH.’/wp-includes/wp-db.php’);

    then in the code I have…
    $result = $wpdb->get_row(“SELECT SQL STATMENT”);

    …which throws a “Fatal error: Call to a member function on a non-object in …”

    Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter jbjaaz

    (@jbjaaz)

    nevermind. I assumed because…

    $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

    …was at the end of the class file, I didn’t have to create the object. But upon adding the line, the error goes away. Sorry for taking up space in the forum.

    you shouldn’t be creating the database connection yourself in a standard plugin — it is already created. However, you do need to reference it as a global in your functions:

    function blah()
    {
    global $wpdb;

    }

    -d

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wpdb in a plugin’ is closed to new replies.