dongleiqiao2107
2010-11-24 06:18 阅读 61
已采纳

为什么我不能以这种方式访问​​wordpress中新创建的表?

I've already created the table successfully:

function jal_install () {
  global $wpdb;
  global $jal_db_version;
  $table_name = $wpdb->prefix . "liveshoutbox";
  if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
      $sql = "CREATE TABLE " . $table_name . " (

id mediumint(9) NOT NULL AUTO_INCREMENT, time bigint(11) DEFAULT '0' NOT NULL, name tinytext NOT NULL, text text NOT NULL, url VARCHAR(55) NOT NULL, UNIQUE KEY id (id) );";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
     dbDelta($sql);
     $welcome_name = "Mr. Wordpress";
     $welcome_text = "Congratulations, you just completed the installation!";
     $insert = "INSERT INTO " . $table_name .
           " (time, name, text) " .
           "VALUES ('" . time() . "','" . $wpdb->escape($welcome_name) . "','" . $wpdb->escape($welcome_text) . "')";
     $results = $wpdb->query( $insert );
  }
}

jal_install ();

But when I try to refer to this table as how WP refers to its internal tables like $wpdb->posts:

var_dump($wpdb->liveshoutbox);

The output is :

null

Why?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    duanliang1898 duanliang1898 2010-11-24 09:21

    There is no support for

    $wpdb->liveshoutbox
    

    and only internal table have direct access to: (eg)

    $wpdb->posts
    

    For that, your only option is:

    $wpdb->prefix . "liveshoutbox"
    
    点赞 评论 复制链接分享
  • doudui1850 doudui1850 2011-05-16 07:52

    I know it's been a while since this thread appeared, but thought I'd share how I solved it. The answer it seems is to refer to the table name in full including the prefix without $wpdb->. Not sure it it works for var_dump, but it did for getting rows and results

    点赞 评论 复制链接分享

相关推荐