走啊走
加油

WordPress建站wpdb操作数据库增删改查的基础用法

服务器价格表

WordPress建站wpdb操作数据库增删改查的基础用法

get_results 查询

global $wpdb;

// 查询条件
$page_author_id = 1;
$post_type_name = 'page';

// 使用 $wpdb->prepare()
$pages = $wpdb->get_results( $wpdb->prepare( 
    "
    SELECT post_title, post_content 
    FROM $wpdb->posts
    WHERE post_author = '%d' 
    AND post_type = '%s'
    ",
    $page_author_id, // %d because it is number
    $post_type_name // %s because it is string
) );

// 输出结果
if( $pages ) {
    foreach ( $pages as $page ) {
        echo $page->post_title;
    }
}
PHP

insert 插入数据

global $wpdb;
$table = $wpdb->prefix.'you_table_name';
$data = array('column1' => 'data one', 'column2' => 123);
$format = array('%s','%d');
$wpdb->insert($table,$data,$format);
$my_id = $wpdb->insert_id;
PHP

update 更新数据

$data = [ 'a' => NULL ]; // NULL value.
$format = [ NULL ];  // Ignored when corresponding data is NULL, set to NULL for readability.
$where = [ 'id' => NULL ]; // NULL value in WHERE clause.
$where_format = [ NULL ];  // Ignored when corresponding WHERE data is NULL, set to NULL for readability.
$wpdb->update( $wpdb->prefix . 'my_table', $data, $where, $format, $where_format );
$wpdb->update( $wpdb->prefix . 'my_table', $data, $where ); // Also works in this case.
PHP

query

函数返回一个与选定内容的行数相应的整数。如果发生MySQL错误,函数返回FALSE。(注意:0和FALSE都可能被返回,确保使用正确的比较运算符:equality == vs. identicality ===)。

注意:使用wpdb类中所有可执行SQL查询的函数时,都需要将所有输入内容/inputs进行字符转义(如wpdb->escape($user_entered_data_string))。

<?php $wpdb->query('query'); ?> 
PHP

query:(字符串)你希望执行的SQL语句。

示例:
删除ID为13的文章的“gargle”元关键字和值。

$wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '13' AND meta_key = 'gargle'");
PHP

将页面7设为页面15的父级。

$wpdb->query("UPDATE $wpdb->posts SET post_parent = 7 WHERE ID = 15 AND post_status = 'static'");
PHP

get_var

函数返回一个来自数据库的变量。虽然只返回一个变量,但查询结果会被整体缓存,供后期使用。如果没有查询结果,返回NULL。

<?php $wpdb->get_var('query',column_offset,row_offset); ?> 
PHP

query:(字符串)你希望执行的查询。将该参数设为null会使函数返回上一个查询缓存结果中的具体变量。

column_offset:(整数)预计的数据库表的列数(0为表中第一列)。默认值为0。

row_offset:(整数)预计的数据库表的行数(0为表中第一行)。默认值为0。

示例
检索并返回用户数量。

<?php
$user_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users;"));
echo '<p>User count is ' . $user_count . '</p>';
?>
PHP

检索并返回自定义字段值的总数。

<?php
$meta_key = 'miles';//set this to appropriate custom field meta key
$allmiles=$wpdb->get_var($wpdb->prepare("SELECT sum(meta_value) FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
echo '<p>Total miles is '.$allmiles . '</p>';
?>
PHP

get_row

从某个查询中检索一整行内容,可使用get_row函数。该函数可将行作为对象、关联数组或数值索引数组返回。如果查询返回了多个行,函数只返回指定行,但所有返回的行都将被缓存以供日后使用。

<?php $wpdb->get_row('query', output_type, row_offset); ?> 
PHP

query:(字符串)你希望执行的查询语句。

output_type:
三个预定义的常量之一。默认值为OBJECT。
OBJECT —— 返回的结果以对象形式输出
ARRAY_A ——返回的结果以关联数组形式输出
ARRAY_N —— 返回的结果以数值索引数组形式输出

row_offset:(整数)预计的数据库表的行数(0为表中第一行)。默认值为0。

示例
获取ID为10的链接的所有资料。

$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10");  
PHP

$mylink对象的属性即SQL查询结果的行名称(在该例中,即 $wpdb->links表中的所有行)。

echo $mylink->link_id; // prints "10"  
PHP

使用

$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_A);  
PHP

则会生成一个关联数组:

echo $mylink['link_id']; // prints "10"  
PHP

$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_N);  
PHP

则会生成一个数值索引数组:

echo $mylink[1]; // prints "10"  
PHP

get_col

要选择数据库表中的一列内容,可使用get_col函数。该函数输出一个空间数组,如果查询返回了多个列,函数只返回指定列,但所有返回的列都将被缓存以供日后使用。

<?php $wpdb->get_col('query',column_offset); ?> 
PHP

query:(字符串)你希望执行的查询。将该参数设为null会使函数返回上一个查询的缓存结果中的执行表列。

column_offset:(整数)预计的数据库表的列数(0为表中第一列)。默认值为0。

来源:https://www.anttoweb.com/kb/wordpress-wpdb/