Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using following code in the theme files to get some output from the DB:

<?php $querystr = "SELECT DISTINCT meta_value FROM $wpdb->wp_postmeta WHERE 'meta_key' LIKE 'movie_name' ORDER BY meta_value ASC";
$movie_names =  $wpdb->get_results($querystr, OBJECT);
echo sizeof($movie_names);
?>
<ul>
<?php foreach ( $movie_names as $movie_name ){ ?>
   <li><?php echo $movie_name->meta_value; ?> </li>
<?php } ?>
</ul>

Above code's result is "0" at the frontend. But when I am running the same query i.e.

SELECT DISTINCT meta_value FROM wp_postmeta WHERE 'meta_key' LIKE 'movie_name' ORDER BY meta_value ASC

in DB then it's returning 6 rows. Please help me in finding the problem in the code.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

$wpdb->wp_postmeta is wrong. Always use just $wpdb->postmeta – without further prefixes.

So …

global $wpdb;

$querystr = "
SELECT DISTINCT meta_value 
    FROM $wpdb->postmeta 
    WHERE 'meta_key' LIKE 'movie_name' 
    ORDER BY meta_value ASC";

$movie_names =  $wpdb->get_results($querystr, OBJECT);

if ( ! $movie_names  )
{
    $wpdb->print_error(); // get the last error message for debugging;
}
else
{
    // do something awesome with the movies
}
share|improve this answer
    
Thanks Toscho. Its working now with some little modification. –  Akhilesh Apr 24 '13 at 16:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.