wordpress:全局wpdb但找不到get_result

时间:2014-08-31 09:07:47

标签: php wordpress global fatal-error wpdb

我有这个功能

function get_project_id($id = 0){
          global $wpdb;
          $q = $wpdb->get_result( 'SELECT * FROM clients_projects WHERE id IN ( '.$id.' ) ' );

      return $q ;
  }

错误就是这个

致命错误:调用未定义的方法wpdb :: get_result

我知道这个问题因为我需要wpdb作为全局

但它已经全球化了

当我print_r($ wpdb);

结果就是这个

 wpdb Object ( [show_errors] => [suppress_errors] => [last_error] => [num_queries] => 23 [num_rows] => 0 [rows_affected] => 0 [insert_id] => 0 [last_query] => SELECT option_value FROM wp_options WHERE option_name = 'auto_core_update_failed' LIMIT 1 [last_result] => Array ( ) [result:protected] => mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 0 [type] => 0 ) [col_info:protected] => [queries] => [reconnect_retries:protected] => 5 [prefix] => wp_ [base_prefix] => wp_ [ready] => 1 [blogid] => 0 [siteid] => 0 [tables] => Array ( [0] => posts [1] => comments [2] => links [3] => options [4] => postmeta [5] => terms [6] => term_taxonomy [7] => term_relationships [8] => commentmeta [9] => useronline ) [old_tables] => Array ( [0] => categories [1] => post2cat [2] => link2cat ) [global_tables] => Array ( [0] => users [1] => usermeta ) [ms_global_tables] => Array ( [0] => blogs [1] => signups [2] => site [3] => sitemeta [4] => sitecategories [5] => registration_log [6] => blog_versions ) [comments] => wp_comments [commentmeta] => wp_commentmeta [links] => wp_links [options] => wp_options [postmeta] => wp_postmeta [posts] => wp_posts [terms] => wp_terms [term_relationships] => wp_term_relationships [term_taxonomy] => wp_term_taxonomy [usermeta] => wp_usermeta [users] => wp_users [blogs] => [blog_versions] => [registration_log] => [signups] => [site] => [sitecategories] => [sitemeta] => [field_types] => Array ( [post_author] => %d [post_parent] => %d [menu_order] => %d [term_id] => %d [term_group] => %d [term_taxonomy_id] => %d [parent] => %d [count] => %d [object_id] => %d [term_order] => %d [ID] => %d [comment_ID] => %d [comment_post_ID] => %d [comment_parent] => %d [user_id] => %d [link_id] => %d [link_owner] => %d [link_rating] => %d [option_id] => %d [blog_id] => %d [meta_id] => %d [post_id] => %d [user_status] => %d [umeta_id] => %d [comment_karma] => %d [comment_count] => %d [active] => %d [cat_id] => %d [deleted] => %d [lang_id] => %d [mature] => %d [public] => %d [site_id] => %d [spam] => %d ) [charset] => utf8 [collate] => [dbuser:protected] => root [dbpassword:protected] => [dbname:protected] => wordpress [dbhost:protected] => localhost [dbh:protected] => mysqli Object ( [affected_rows] => 0 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 1 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.6.17 [server_version] => 50617 [stat] => Uptime: 39797 Threads: 1 Questions: 8411 Slow queries: 0 Opens: 616 Flush tables: 1 Open tables: 77 Queries per second avg: 0.211 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 344 [warning_count] => 0 ) [func_call] => $db->query("SELECT option_value FROM wp_options WHERE option_name = 'auto_core_update_failed' LIMIT 1") [is_mysql] => 1 [incompatible_modes:protected] => Array ( [0] => NO_ZERO_DATE [1] => ONLY_FULL_GROUP_BY [2] => STRICT_TRANS_TABLES [3] => STRICT_ALL_TABLES [4] => TRADITIONAL ) [use_mysqli:wpdb:private] => 1 [has_connected:wpdb:private] => 1 [categories] => wp_categories [post2cat] => wp_post2cat [link2cat] => wp_link2cat [useronline] => wp_useronline )  

但get_result不起作用及其

致命错误:调用未定义的方法wpdb :: get_result

1 个答案:

答案 0 :(得分:1)

使用$wpdb->get_results()代替$wpdb->get_result() - 您错过了" s"在方法的最后。

此外,您可以使用以下$wpdb方法,这些方法在您的情况下更有用:

  • get_row()
  • get_col()
  • get_var()