Update Query Tosses syntax error, unexpected T_CONSTANT_ENCAPSULATED_STRING

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?
 
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:
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 ) );
 
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)?
 
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)?

Yes, I believe that the transition began around WP 3.2 with requirements becoming MySQL 5 +.
 
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
 
  • Like
Reactions: LPH
Back
Top Bottom