• Resolved ABlueEyedMonkey

    (@ablueeyedmonkey)


    Hi guys, this is my first post here, and i promise ive done a lot of research before deciding to seek help; I really am totally lost now. Basically I have written a plugin that updates a score value in my database.

    Very simply, i use an add_action and within the function i call a wpdb->update. I print the value just before and just after it writes to the database and the echo is correct. However when i look in the database with phpmyadmin i see that whatever the SQL i used, it doubles itself. So if I try to increment a column by 1, it does it twice. If i use an insert, it inserts it twice. Even if I put a die straight after it, it still does it <:(

    Here is the code exactly as it is:

    add_action( "loop_start", "start" );
    function start( $post )
    {
    	global $wpdb;
    	$USER_ACHIEVEMENTS_DB = $wpdb->prefix.PG_USER_ACHIEVEMENTS;
    	$userAchievement = $wpdb->get_row("SELECT * FROM $USER_ACHIEVEMENTS_DB WHERE achievement = 'Explorer' AND user_id = 1");
    	$currentScore =(int)$userAchievement->score;
    
    	//Print just before
    	echo "before $currentScore";
    	$currentScore += 1; //increment
    
    	//Call the update
    	$wpdb->update( "$USER_ACHIEVEMENTS_DB", array( 'stage' => 1, 'level' => $currentScore, 'score' => $currentScore ),
    			array( 'user_id' => 1, 'achievement' => "Explorer" ),
    			array( '%d', "%s", '%d', ),
    			array( '%d', '%s' ));
    
    	echo "after $currentScore"; //This is correct. but in the DB its always double what I increment it with.
    	die("FIN");
    }

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

    (@ablueeyedmonkey)

    Ok i managed to find a solution, but I still don’t understand why it happened. If i change my code to:

    add_action( "loop_start", "start" );
    function start( $post )
    {
    	if(is_singular())
    	{
    	global $wpdb;
    	$USER_ACHIEVEMENTS_DB = $wpdb->prefix.PG_USER_ACHIEVEMENTS;
    	$userAchievement = $wpdb->get_row("SELECT * FROM $USER_ACHIEVEMENTS_DB WHERE achievement = 'Explorer' AND user_id = 1");
    	$currentScore =(int)$userAchievement->score;
    
    	//Print just before
    	echo "before $currentScore";
    	$currentScore += 1; //increment
    
    	//Call the update
    	$wpdb->update( "$USER_ACHIEVEMENTS_DB", array( 'stage' => 1, 'level' => $currentScore, 'score' => $currentScore ),
    			array( 'user_id' => 1, 'achievement' => "Explorer" ),
    			array( '%d', "%s", '%d', ),
    			array( '%d', '%s' ));
    
    	echo "after $currentScore"; //This is correct. but in the DB its always double what I increment it with.
    	die("FIN");
    	}
    	else
    	{
    	echo "This never gets called??"; //Why if i add the singular check does this never get called?
    	}
    }

    Then it works ok, but the else never gets called. So how can this function be getting called twice?

    – post retracted…. I commented on the wrong one.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query doubling up on loop_start’ is closed to new replies.