WordPress $wpdb->get_row() Query Won’t Work With Variable, But Works With Hardcoded

My code is correct (I’ve been programming for 13 years), but for some reason when I use a variable in a SELECT query in the WHERE clause, I get no result. If I hardcode it, it works. How could this be? Blowing my mind…

$track = $wpdb->get_row($wpdb->prepare("SELECT tracking_id, order_id, outbound_tracking_number, return_tracking_numbers FROM wp_woocommerce_trackingnumbers WHERE order_id = %s", $o_id));

Thanks!

Related posts

Leave a Reply

1 comment

  1. I think it should be (%s should be %d)

    $track = $wpdb->get_row($wpdb->prepare("SELECT tracking_id, order_id, outbound_tracking_number, return_tracking_numbers FROM wp_woocommerce_trackingnumbers WHERE order_id = %d", $o_id));
    

    %s for strings and %d for digits and variable $o_id sounds like an integer to me.