$wpdb->准备返回空数组

时间:2016-10-22 作者:kleinermann

出于某种原因,此函数中的查询返回一个空数组。我已经看这个太久了,不知道为什么。有人有主意吗?我是否正确使用$wpdb->准备?

function getComponents($page_id, $currentLanguage) {
       global $wpdb;
       $result = $wpdb->get_results($wpdb->prepare(
          \'SELECT id, post_content, post_title, post_excerpt, post_name, m1.meta_value AS `template`, m2.meta_value AS `home_description` \' .
          \'FROM \' . $wpdb->posts . \' p \' .
          \'JOIN wp_term_relationships r ON p.ID=r.object_id \' .
          \'JOIN wp_terms t ON r.term_taxonomy_id=t.term_id \' .
          \'WHERE t.slug=%s AND post_parent=%d AND post_type="page" AND post_status="publish"\' .
          \'ORDER BY menu_order ASC \',
          $currentLanguage, $page_id
       ), OBJECT_K);
       return $result;
    }

1 个回复
SO网友:Rafal

Try with:

function getComponents($page_id, $currentLanguage) {
       global $wpdb;
       $result = $wpdb->get_results($wpdb->prepare( "
          SELECT id, post_content, post_title, post_excerpt, post_name, m1.meta_value AS \'template\', m2.meta_value AS \'home_description\'
          FROM {$wpdb->posts} p
          JOIN wp_term_relationships r ON p.ID = r.object_id
          JOIN wp_terms t ON r.term_taxonomy_id = t.term_id
          WHERE t.slug=%s AND post_parent = %d AND post_type=\'page\' AND post_status = \'publish\'
          ORDER BY menu_order ASC ",
          $currentLanguage, $page_id ), OBJECT_K );
       return $result;
    }

相关推荐

$wpdb->get_row返回什么?

由于插件中的调试和错误查找,我有一个简短的问题。$wpdb->;如果没有结果,返回get\\u row()?文件说明:(array|object|null|void) Database query result in format specified by $output or null on failure. 但当查看源代码时,它似乎返回了;null“;。。。如果您能快速提供帮助,我将不胜感激,我一直认为它会返回一个空数组,但我可能弄错了!