我的$wpdb准备有什么问题?

时间:2014-08-14 作者:user995317

我无法理解这件事。我收到以下错误:

Parse error: syntax error, unexpected \'"\' in 224. 这条线是foreach $html:

$html = \'\';
   foreach ( $recent_across_network as $post ) {
      $html .= \'blog_id, $post->ID ) . \'">\' . $post->post_title . \'\';
   }
   $html .= \'\';
我还发现错误$wpdb-prepare:

$site_list = $wpdb->get_results( $wpdb->prepare(\'SELECT * FROM wp_blogs ORDER BY blog_id\') );
prepare 我相信需要争论吗?所以我尝试了这个:

$site_list = $wpdb->get_results( $wpdb->prepare(\'SELECT * FROM wp_blogs ORDER BY blog_id = %d\', $wpdb->blogid) );
但这也行不通。

编辑:代码基于本文中的函数“wp\\u recent\\u over\\u network”:http://www.smashingmagazine.com/2011/11/17/wordpress-multisite-practical-functions-methods/

3 个回复
SO网友:Welcher

这些看起来像两个独立的问题。

我认为第一个是单引号与双引号的问题:

尝试以下操作:

$html = \'\';
foreach ( $recent_across_network as $post ) {
    $html .= \'blog_id, \'.$post->ID.\' ) . \'">\' . $post->post_title . \'\';
}
$html .= \'\';
foreach中的行将$post->ID放在单引号中,单引号不会计算它的值,而是将精确的字符串放在$html中。

对于准备问题,请尝试将查询用双引号括起来,并传递一个实际值作为参数。

此外,您应该通过帮助器访问表,而不是直接访问,因为表前缀不是wp\\uu,您的查询将无法工作。阅读WPDB 类引用,了解prepare、表名等详细信息

$site_list = $wpdb->get_results( $wpdb->prepare(\'SELECT * FROM wp_blogs ORDER BY blog_id = %d\', $wpdb->blogid) );
//becomes
$site_list = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->blogs ORDER BY blog_id = %d", 1) );
免责声明-我还没有测试过!

希望有帮助

SO网友:Pat J

您不需要调用$wpdb->prepare() 因为原始SQL查询不包含用户的任何输入。您的第二个查询--SELECT * FROM wp_blogs ORDER BY blog_id = %d -- 出现语法错误(ORDER BY 子句不接受= %d), 这样就行不通了。

(至少我很确定是这样的。我在the MySQL docs 表明ORDER BY blog_id=%d 无论如何,这会起作用。)

此外,如果您试图在多站点安装中获取站点列表,我建议使用wp_get_sites() 而不是重新发明轮子。

SO网友:Stack Programmer

我正在重写前面提到的整个函数“wp\\u recent\\u over\\u network”。

1) With Using Transients API

function wp_recent_across_network( $size = 10, $expires = 7200 ) {
    if( !is_multisite() ) return false;

    // Cache the results with the WordPress Transients API
    // Get any existing copy of our transient data
    if ( ( $recent_across_network = get_site_transient( \'recent_across_network\' ) ) === false ) {

        // No transient found, regenerate the data and save a new transient
        // Prepare the SQL query with $wpdb
        global $wpdb;

        // Because the get_blog_list() function is currently flagged as deprecated
        // due to the potential for high consumption of resources, we\'ll use
        // $wpdb to roll out our own SQL query instead. Because the query can be
        // memory-intensive, we\'ll store the results using the Transients API
        if ( false === ( $site_list = get_site_transient( \'multisite_site_list\' ) ) ) {
            $site_list = $wpdb->get_results( \'SELECT * FROM \' . $wpdb->prefix . \'blogs ORDER BY blog_id\' );
            set_site_transient( \'multisite_site_list\', $site_list, $expires );
        }

        $limit = absint($size);

        // Merge the wp_posts results from all Multisite websites into a single result with MySQL "UNION"
        foreach ( $site_list as $site ) {
            if( $site == $site_list[0] ) {
                $posts_table = $wpdb->prefix . "posts";
            } else {
                $posts_table = $wpdb->prefix . $site->blog_id . "_posts";
            }

            $posts_table = esc_sql( $posts_table );
            $blogs_table = esc_sql( $wpdb->prefix . \'blogs\' );

            $query .= "(SELECT $posts_table.ID, $posts_table.post_title, $posts_table.post_date, $blogs_table.blog_id FROM $posts_table, $blogs_table";
            $query .= " WHERE $posts_table.post_type = \'post\'";
            $query .= " AND $posts_table.post_status = \'publish\'";
            $query .= " AND $blogs_table.blog_id = {$site->blog_id})";

            if( $site !== end($site_list) )
                $query .= " UNION ";
                else
                    $query .= " ORDER BY post_date DESC LIMIT 0, $limit";
        }

        // Sanitize and run the query
        $recent_across_network = $wpdb->get_results( $query );

        // Set the Transients cache to expire every two hours
        set_site_transient( \'recent_across_network\', $recent_across_network, 60*60*2 );
    }
    // Format the HTML output
    if($recent_across_network) {
        $html = \'<ul>\';
        foreach ( $recent_across_network as $post ) {
            $html .= \'<li><a href="\'.get_blog_permalink($post->blog_id, $post->ID).\'">\'.$post->post_title.\'</a></li>\';
        }
        $html .= \'</ul>\';
        echo $html;
    }
}

2) Without Using Transients API

function wp_recent_across_network1( $size = 10) {
    if( !is_multisite() ) return false;

    // Prepare the SQL query with $wpdb
    global $wpdb;
    $site_list = $wpdb->get_results( \'SELECT * FROM \' . $wpdb->prefix . \'blogs ORDER BY blog_id\' );

    $limit = absint($size);

    // Merge the wp_posts results from all Multisite websites into a single result with MySQL "UNION"
    foreach ( $site_list as $site ) {
        if( $site == $site_list[0] ) {
            $posts_table = $wpdb->prefix . "posts";
        } else {
            $posts_table = $wpdb->prefix . $site->blog_id . "_posts";
        }

        $posts_table = esc_sql( $posts_table );
        $blogs_table = esc_sql( $wpdb->prefix . \'blogs\' );

        $query .= "(SELECT $posts_table.ID, $posts_table.post_title, $posts_table.post_date, $blogs_table.blog_id FROM $posts_table, $blogs_table";
        $query .= " WHERE $posts_table.post_type = \'post\'";
        $query .= " AND $posts_table.post_status = \'publish\'";
        $query .= " AND $blogs_table.blog_id = {$site->blog_id})";

        if( $site !== end($site_list) )
            $query .= " UNION ";
            else
                $query .= " ORDER BY post_date DESC LIMIT 0, $limit";
    }

    // Sanitize and run the query
    $recent_across_network = $wpdb->get_results( $query );

    // Format the HTML output
    if($recent_across_network) {
        $html = \'<ul>\';
        foreach ( $recent_across_network as $post ) {
            $html .= \'<li><a href="\'.get_blog_permalink($post->blog_id, $post->ID).\'">\'.$post->post_title.\'</a></li>\';
        }
        $html .= \'</ul>\';
        echo $html;
    }
}
下面列出了所做的更改

无需调用$wpdb->prepare() 因为查询中没有传递变量,所以更正了参考网站中的错误代码,将前缀变量更改为$wpdb->prefix

结束