문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top