WordPress.org

Ready to get started?Download WordPress

Forums

$wpdb->prepare with variable (2 posts)

  1. Steppppo
    Member
    Posted 5 years ago #

    I am trying to use a variable with wpdb->prepare but it's not working for some reason.

    I've built this statement in my code:

    "UPDATE wp_eventuser_tbl SET fname = %s, lname = %s, active = %d WHERE id = %d", Steven, Nowickcow, 0, 10

    and set it equal to $strQuery.

    but, when i execute this line:

    $wpdb->query( $wpdb->prepare( $strQuery ) );

    it doesn't work.

    however, I can cut and past the statement exactly and replace $strQuery and the code updates perfectly.

    any clues as to why it won't execute using $strQuery?

  2. dpolant
    Member
    Posted 4 years ago #

    You have to do it like this:

    $strQuery = "UPDATE wp_eventuser_tbl SET fname = %s, lname = %s, active = %d WHERE id = %d";
    
    $wpdb->query($wpdb->prepare( $strQuery, "Steven", "Nowickcow", 0, 10 ) );

    You can't set a variable equal to a quoted string plus a list of unquoted values. PHP doesn't understand that you want to move that whole thing as a bunch of different arguments for $wpdb->prepare.

Topic Closed

This topic has been closed to new replies.

About this Topic