1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Update Query Tosses syntax error, unexpected T_CONSTANT_ENCAPSULATED_STRING

Discussion in 'General PHP and MySQL Discussions' started by LPH, Feb 3, 2014.

  1. LPH

    LPH Well-Known Member

    This query is written wrong but every attempt to re-write it causes the update to the database to fail.

    Code:
    $wpdb->query("UPDATE $wpdb->posts SET thread_id = '$thread_id' WHERE ID = '{$post->ID}'");
    The first thing is prepare is not being used but I became confused about %s (string) and %d (integer).

    The second thing is the double quotes cannot be used in this manner. I know the issue is in the {post->ID} but not sure how to fix it.

    Does anyone have any suggestions?
     
  2. LPH

    LPH Well-Known Member

    Updated Post:

    This was my start of modification but does not use %s and %d. This code works but I'm not sure how to use the %s and %d....

    Code:
    $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET thread_id = '$thread_id' WHERE ID = '{$post->ID}'" ) );
    
    How can this be changed?
     
    Last edited: Feb 4, 2014
  3. LPH

    LPH Well-Known Member

    I feel so stupid :( . I read that dang PHP manual, and dozens of websites, until I started picking through it and realized that both are %d. Next, I removed the curly braces (not in double quotes any longer) - and this appears to work - no errors and the preparing of the query looks right.

    Code:
    $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET thread_id = %d WHERE ID = %d", $thread_id, $post->ID ) );
     
    Digital Doctor likes this.
  4. digitalpoint

    digitalpoint Well-Known Member

    Hmmmm... total side note, but that looks like WordPress using MySQLi to me... is WordPress finally supporting MySQLi (rather than the old deprecated MySQL extension)?
     
  5. LPH

    LPH Well-Known Member

    Yes, I believe that the transition began around WP 3.2 with requirements becoming MySQL 5 +.
     
  6. digitalpoint

    digitalpoint Well-Known Member

    Well I know WordPress 3.5 didn't have mysqli support yet. Are you using a third-party database class or anything?

    Edit - looks like they use their own prepare() method on top of mysql (not mysqli) still... boo. Really would be nice if we weren't forced to compile in deprecated PHP extensions for WordPress. lol
     
    LPH likes this.
  7. digitalpoint

    digitalpoint Well-Known Member

Share This Page