Search code examples
phpsqlwordpresssyntaxnextgen-gallery

NextGen plugin and SQL syntax error


I am trying to get the NextGen gallery Id dynamically and getting SQL syntax error

#1064 - You have an error in your SQL syntax

Have a look on below code where I am making mistake

<?php
global $wpdb;
global $post;
$galleryid = get_post_meta( $post->ID, 'image_gallery', true ); 
$pictures=$wpdb->get_results("SELECT * FROM wp_ngg_pictures WHERE galleryid='$galleryid'");
?>

Or may be I am doing it wrong way?


Solution

  • It's quite likely that $galleryid has an unexpected value, but nevertheless you should use the prepare() method that Wordpress provides for your queries:

    $pictures = $wpdb->get_results( 
        $wpdb->prepare( 
            "SELECT * FROM wp_ngg_pictures WHERE galleryid = %d",
            $galleryid
        )
    );
    

    If you want to test the query, you can take the output of prepare() and run that in phpMyAdmin.