WordPress.org

Ready to get started?Download WordPress

Forums

sql update in wpdb: increment a column (2 posts)

  1. Jonas_
    Member
    Posted 2 years ago #

    I want to update the views only when certain things are shown... so i've a code for that.. but now to update it.. would this work???

    $wpdb->update( $table, array( 'views' => 'views'+1 ), array( 'id' => $new_result->id ) ); ?>

    or how do i increment it with the wpdb syntax?

    Thanks in advance!

  2. Kakoma
    Member
    Posted 2 years ago #

    Jonas, you may want to consider retrieving the value of 'views' using a select:
    e.g
    $view = $wpdb->get_var( $wpdb->prepare( "SELECT view FROM $table WHERE id='$new_result->id';" ) );
    then incrementing it
    $views = $view+1;
    then running your update above with the new value

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.