• Resolved bungeebones

    (@bungeebones)


    I took this example right out of the codex and it works fine …

    $wpdb->query(
    	"
    	UPDATE $wpdb->posts
    	SET post_parent = 7
    	WHERE ID = 15
    		AND post_status = 'static'
    	"
    );

    I even modified it to this …

    $wpdb->query(
    	"
    	UPDATE $wpdb->posts
    	SET post_parent = 2
    	WHERE ID = 12	"
    );

    BUT if I run the following adaptation I get an error and no update …

    $wpdb->query("UPDATE $wpdb->bungeebones SET greeting = 2 WHERE id = 1 ");
    bungeebones is the table name and wp_ is the prefix so I tried wp_bungeebones as well.

    I get the following error …

    Error: [8] Undefined property: wpdb::$bungeebonesstring(38) “UPDATE SET greeting = 2 WHERE id = 1 ”

    This is driving me nuts as the sample and my code look the same to me. I’ve looked and looked for some info on this ” Undefined property: wpdb::” error message but can’t find anything. Can someone help me please? What am I missing?

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • try to add global $wpdb
    before the query

    global $wpdb
    $wpdb->query("YOUR QUERY);

    Thread Starter bungeebones

    (@bungeebones)

    Thanks Tim, but that isn’t the issue. I had $wpdb as global at the beginning and, note that the sample code works fine with the same db connection. No, there is some other bizarre problem.

    I redid the test code messages and noticed something different in the query produced — it is dropping the table name?

    Here is the test code again – the first query doesn’t work, the second (sample code) does…

    global $wpdb;
    $wpdb->query("UPDATE $wpdb->bungeebones SET greeting = 2 WHERE id = 1 ");
    echo '<br>';
    var_dump($wpdb->last_query);
    echo '<br>';
    $wpdb->query("UPDATE $wpdb->posts 	SET post_parent = 5	WHERE ID = 12 	");
    echo '<br>';
     exit(var_dump($wpdb->last_query));

    That produces these messages:

    Error: [8] Undefined property: wpdb::$bungeebones
    string(38) “UPDATE SET greeting = 2 WHERE id = 1 “

    string(52) “UPDATE wp_posts SET post_parent = 5 WHERE ID = 12 ”

    Notice the table name “bungeebones” has been completely dropped from the query?

    The only other detail is that there is a “wp_” table prefix but it is omitted from the sample query (which works). The code I am trying to get to work is not affected by changing the prefix except it will show the new name as the one which is “undefined”. There is nothing antwhere on how to “define” a property?

    Thread Starter bungeebones

    (@bungeebones)

    Ok, I found the fix but don’t have a clue why it works …

    I used the following code to place the table name in a variable

    $prefix = $wpdb->prefix;
       $table_name =  $prefix.'bungeebones';

    Then I used that variable in the query. Where it was

    $wpdb->query("UPDATE $wpdb->wp_bungeebones SET greeting = 2 WHERE id = 1 ");

    it was changed to:
    $wpdb->query("UPDATE $table_name SET greeting = 2 WHERE id = 1 ");

    owh, it’s about prefix.

    so, you can change to

    $wpdb->query("UPDATE $wpdb->prefix.wp_bungeebones SET greeting = 2 WHERE id = 1 ");

    Database_Table_Prefix

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Updating DB table error’ is closed to new replies.