在 Wordpress 插件中进行数据库操作

来源:互联网 发布:淘宝店铺流量分析 编辑:程序博客网 时间:2024/05/01 13:08

    在 WP 中操作数据,主要有两种方法:

    1. 使用 Options 系列函数,把数据以“变量-数值”对的形式,保存到 wp_options 表中。
    这种方法适合保存一些字典类型的数据,比如插件的配置信息等。

    2. 创建用户自己的表,或操作系统的表。

    第一种

    使用 WordPress Options,往数据库存储和取回数据,与函数调用一样方便。WP 为 options 提供了四个函数:

    - add_option
    - get_option
    - update_option
    - delete_option
    -add_option

    add_option 函数接受四个参数,格式为:add_option($name,$value,$description,$autoload)。使用这个函数来添加数据是很有好处的。参数 $name 必须是独一无二的,否则你就会覆盖别人的option,或者别人会覆盖你的option。

    get_option 函数允许取回已经存储在数据库里的 option。它只接受一个参数,就是option 的名字。函数的格式是:get_option($option_name)。

    update_option 函数工作方式和 add_option 是类似的,除此之外,如果 option 已经存在,该函数会更新option 的值。当往数据库中存储数据的时候,也可以使用这个双重功能的函数。

    delete_option 函数从数据库中删除 options。函数的格式是: delete_option($option_name)。

    第二种

    另一种在 WordPress 数据库存取数据的强大的方法是使用 WordPress 数据库类对象 $wpdb。在一个函数中,这个类对象的引用方式如下:

    function sample_function()
    {
      global $wpdb;
      ……
    }

    示例,取回 WordPress 博客评论的总数:

    function sample_function()
    {
      global $wpdb;
      $comments = $wpdb->get_row("SELECT COUNT(comment_approved) AS comments_count FROM
                  $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_approved", ARRAY_A);
      echo $comments['comments_count'];
    }

    wpdb 类是一个有着许多功能的非常强大的类。可以打开网址 http://codex.wordpress.org/Function_Reference/wpdb_Class 查看其更多说明。中文翻译文档地址:http://codex.wordpress.org/zh-cn:Main_Page。

    wpdb 类的其它方法:

    $wpdb->query()
    $wpdb->get_var()
    $wpdb->get_row()
    $wpdb->get_col()
    $wpdb->get_results()
    $wpdb->insert()
    $wpdb->update()
    $wpdb->get_col_info()
    $wpdb->flush()
    $wpdb->show_errors()
    $wpdb->hide_errors()
    $wpdb->print_error()

    类变量:

    $show_errors:
    Whether or not Error echoing is turned on. Defaults to TRUE.

    $num_queries:
    The number of queries that have been executed.

    $last_query:
    The most recent query to have been executed.

    $queries:
    You may save all of the queries run on the database and their stop times by setting the SAVEQUERIES constant to TRUE (this constant defaults to FALSE). If SAVEQUERIES is TRUE, your queries will be stored in this variable as an array.

    $last_result:
    The most recent query results.

    $col_info:
    The column information for the most recent query results. See Getting Column Information.

    $insert_id:
    ID generated for an AUTO_INCREMENT column by the most recent INSERT query.

    $num_rows:
    The number of rows returned by the last query.

    还可以返回涉及的表名:

    $posts:
    The table of Posts.

    $users:
    The table of Users.

    $comments:
    The Comments table.

    $links:
    The table of Links.

    $options:
    he Options table.

    $postmeta:
    The Meta Content (a.k.a. Custom Fields) table.

    $usermeta:
    The usermeta table contains additional user information, such as nicknames, descriptions and permissions.

    $terms:
    The terms table contains the 'description' of Categories, Link Categories, Tags.

    $term_taxonomy:
    The term_taxonomy table describes the various taxonomies (classes of terms). Categories, Link Categories, and Tags are taxonomies.

    $term_relationships:
    The term relationships table contains link between the term and the object that uses that term, meaning this file point to each Category used for each Post.

    作者:张庆(网眼)
    西安 PHP 教育培训中心(QQ群:108017660)
    来自“网眼视界”:http://blog.why100000.com
    “十万个为什么”电脑学习网:http://www.why100000.com
    2010-5-5