tags:

views:

65

answers:

2

Hi,

I am doing the following:

$type = 'attachment';
$images = $wpdb->get_results($wpdb->prepare('

                SELECT p.*
                FROM wp_%d_posts p
                WHERE p.post_parent =%d
                AND p.post_type = "%s"


    ', $blog_id, $page->ID, $type),OBJECT);


var_dump($images);  

If I remove the line 'AND p.post_type = "%s"' then I get results returned, otherwise I get an empty array returned. If I run the query direct against the DB in a mysql client, I get results.

There is no error, just an empty result set. I am doing similar queries throughout my file and they are working so I'm not looking for "dont do it like that" style replies. I am against a tight deadline and just need to understand why this isnt working and fix it.

PHP 5.3, MYSQL 5.1. Wordpress MU 2.9.2

thanks!

A: 

Do not Quote "%s". From the WordPress site, "Notice that you do not have to worry about quoting strings. Instead of passing the variables directly into the SQL query, use a %s placeholder for strings and a %d placedolder for integers."

Example:

$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d", $var, $id ) );

Gary
Thanks but I tried that and it doesn;t make any difference. I also tried loads of different combinations of quotes, single quotes, escaped quotes. No difference.
codecowboy
Your code as posted is wrong according to the Wordpress manual. I pointed that out, and you mark the answer as wrong? It certainly appears as if you are not handling the Prepare statement properly...
Gary
sorry - if you edit the answer I will vote it back up. Just a bit stressed :-(
codecowboy
A: 

Hi - apologies to all. there is nothing wrong with the query. Its my brain that is wrong.

codecowboy