I am trying to utilize this function. I cannot figure out why I am still getting the PHP error. All I can figure out is that for some reason the template isn't able to access $wpdb. I don't know what line needs to be placed in the functions.php (in theme folder) file that contains this function. Please help me!!

function submitWeightUpdate( $userid, $weight ) {
    global $wpdb;

    $result = $wpdb->insert( 'wp_weights', array( 'user_id' => $userid, 'current_weight' => $weight ), array( '%d', '%d' ) );

    if (empty($_POST['weight'])) {
        $return['error'] = true;
        $return['msg'] = 'You did not enter a weight.';
    }
    else {
        $return['error'] = false;
        $return['msg'] = 'You\'ve entered: ' . $weight . ' as your new weight.';
    }

    echo json_encode($return);
} 

I also tried setting the theme to the default one and it still won't work, so I have no idea what could be wrong with it!

link|improve this question
1  
Please try this var_dump( isset( $wpdb ) ); in function and tell what it outputs. – Rarst Feb 13 '11 at 20:25
Sorry I didn't reply earlier I didn't see your answer. I tried it without globalizing $wpdb, globalizing via " $GLOBALS['wpdb'] = $wpdb;", and globalizing via "global $wpdb". The result was always the same: bool(false). Could it be a permissions issue or something? In order to make the theme files editable inside of WP I had to set them chmod 767, wonder if I need to "reset" the permissions somehow or do some more modifications? – Brandon Feb 14 '11 at 7:32
Hmmm... I am not quite sure why would $wpdb be uninitialized in this function, I assume rest of the site is working normally? How exactly you are running this code? In which template/whatever? – Rarst Feb 14 '11 at 18:23
@Rarst: Those globals are unset mostly for two reasons: A) not yet initialized -or- B) because the object in there has been already destroyed (shutdown). And sure there is C) that someone just NULed/unset it which, well, can happen. – hakre Feb 14 '11 at 18:34
@hakre yeah, I understand technical reasons why it would be unset, I don't get why would it be that way in one specific function if rest of stuff works fine. I suspect this is more of loading some file directly or AJAX stuff. – Rarst Feb 14 '11 at 18:43
show 3 more comments
feedback

2 Answers

You did everything right with the globalize, the error message is just telling you that you called a function on $wpdb which does not exists.

Just check prior you do that $wpdb contains the object you're intersted in:

if (is_object($wpdb) && is_a($wpdb, 'wpdb')) {
    $result = $wpdb->insert( 'wp_weights', array( 'user_id' => $userid, 'current_weight' => $weight ), array( '%d', '%d' ) );
}

Alternatively you can add this below the global line to learn more:

global $wpdb;
var_dump($wpdb); // dump variable type and contents.

Additionally try:

require_once( ABSPATH . 'wp-load.php' );
global $wpdb;

You might not have wordpress ready to provide what you need in $wpdb.

link|improve this answer
In the conversation above the 2 guys figured out that I was using ajax and that was the root of the problem...now I just have to figure out how to properly either use ajax or rewrite my code to be able to pass text from my editbox... – Brandon Feb 14 '11 at 23:49
Using AJAX is not the root of your problem. As using globals wasn't either. – hakre Feb 15 '11 at 0:35
Ok, so your code will probably help. When I use require_once( ABSPATH . 'wp-load.php' ); I get an error about loading ABSPATHwp-load.php but when I change it to ../../../wp-load.php then I get an error saying /httpdocs/wp-content/themes/smartone/wp-admin doesn't exist. I assume wp-load is trying to load files based on the current directory which isn't working right. How do I make it load properly? – Brandon Feb 15 '11 at 0:39
wp-load.php does only do what's coded inside of it. Just open the file with your editor, read the source-code in there and you will learn all you need to know. – hakre Feb 15 '11 at 0:48
The exact error is "Failed opening required 'ABSPATHwp-load.php' (include_path='.:')" – Brandon Feb 15 '11 at 0:54
show 7 more comments
feedback

It's likely that $wpdb has been globalized when it hits your particular function. Try checking $GLOBALS and if it there is an entry for $wpdb remove the line $global $wpdb; at the start of your funciton.

link|improve this answer
Thank you for your reply. I searched the themes functions.php file for "GLOBALS" and this is the only thing I came up with, I included the line above it to you could get an idea where it was located: function smartone_comment( $comment, $args, $depth ) { $GLOBALS['comment'] = $comment; I got the idea to try declaring it that way though when you said that so I changed "global $wpdb" to "$GLOBALS['wpdb'] = $wpdb;" and I now get "PHP Notice: Undefined variable: wpdb" as well as the original error. – Brandon Feb 13 '11 at 17:14
So, if you do a print_r of GLOBALS at the start of your function -- what is the output? – Ethan Seifert Feb 13 '11 at 20:36
Ok I did it, I have uploaded the results to link. If I have the "$GLOBALS['wpdb'] = $wpdb;" line uncommented it adds "[wpdb] => " at the bottom of the list I uploaded. – Brandon Feb 13 '11 at 21:10
@Brandon - Your output shows that global $wpdb is unset. Just in case you have not yet realized. There is no 'wpdb' entry in the print_r $GLOBALS output you posted. – hakre Feb 14 '11 at 18:38
I know, if I add the "global $wpdb;" line it adds "[wpdb] => " at the bottom of the list I uploaded. – Brandon Feb 14 '11 at 23:47
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.