0

我正在尝试动态获取 NextGen 画廊 ID 并出现 SQL 语法错误

#1064 - You have an error in your SQL syntax

看看下面我犯错的代码

<?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'");
?>

还是我做错了?

4

1 回答 1

1

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.

于 2013-10-23T13:31:55.723 回答