WordPress.org

Support

Support » How-To and Troubleshooting » $wpdb->prepare with variable

$wpdb->prepare with variable

  • 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?

Viewing 1 replies (of 1 total)
  • 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.

Viewing 1 replies (of 1 total)
  • The topic ‘$wpdb->prepare with variable’ is closed to new replies.