• Resolved chiconerd

    (@chiconerd)


    alright so i was looking at how to manage queries in wordpress and i came across wpdb, it says that it should work just fine with no modification.

    but when i try to use a query it gives me a fatal error, and tells me im working with a non-object..

    i try many different things and the only thing that works is if i call the class and put in all the database values manually every time..
    this a security risk..

    can anyone help me?

    why is not working like the guide says it should.

Viewing 6 replies - 1 through 6 (of 6 total)
  • MichaelH

    (@michaelh)

    What are you trying to query?

    While wpdb may be useful, if you are querying posts you might look at the template tag, query_posts(), or get_posts().

    chiconerd

    (@chiconerd)

    nope just a normal query
    when i use this

    <?php
        $wpdb = $wpdb->get_results('SELECT * FROM costume_table');
    ?>

    this will produce this error

    Fatal error: using a property of a non-object;

    how ever if i do this

    <?php
        $wpdb = new wpdb('user','password','database','host');
        $wpdb = $wpdb->get_results('SELECT * FROM costume_table');
    ?>

    and that is the problem i have to include the new class in every new page i make, i thought the class was included within wordpress, putting mysql info in every page does not sound safe. :S

    MichaelH

    (@michaelh)

    If you put the database tables you are accessing in the save database as the rest of WordPress tables you should be able to access them with the wpdb class.

    chiconerd

    (@chiconerd)

    yeah the problem is not accessing the database, is running the code.
    the code wont run unless i create an instance of the class in every page i want to use the wpdb class.

    as i explained before, in the documentation it says that automatically wordpress enable this functionally. but even if i try to use one example provided by wordpress i get this error.

    this the code wordpress provides

    <?php
    $name = $wpdb->get_var("SELECT name FROM $wpdb->terms WHERE term_ID=1");
    echo $name;
    
    ?>

    this is the error it produces
    Fatal error: Call to a member function get_var() on a non-object in C:\sites\wordpress\wp-content\plugins\exec-php\includes\runtime.php(42) : eval()'d code on line 2

    chiconerd

    (@chiconerd)

    well i found another way to do this in every page i create instead of using the new class. by using

    global $wpdb;

    the code works.

    still not sure why in the guide they don’t address this error.

    MichaelH

    (@michaelh)

    Even though wpdb does refer to making $wpdb global, can you recommend a better method of explaining that?

    Thanks for your recommendations.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘WPDB’ is closed to new replies.