使用wordpress的时候,如果想直接使用wp里封装的数据库操作的类(wp-db.php),将wp-blog-header.php包含到代码中就可以使用了。
define(‘path’, dirname(dirname(__file__)).‘/’); require_once(path . ‘../wp-blog-header.php’); global $wpdb;
插入数据时,其中一种方法是使用wp-db类中的insert()函数。
$table = test_table;$data_array = array(‘column_1′ => ‘data1′,‘column_2′ => ‘data2′);$wpdb->insert($table,$data_array);
第一个参数是数据库表中的名字,第二个参数是要插入的数据,是一个数组。数组中的key的名字就是表中的列名。其实insert()函数还有第三个参数format,感兴趣的朋友可以在wp-db.php的方法定义里看看更新数据时,可以用update()函数,例如:
$table = test_table;$data_array = array( ‘column_1′ => ‘new_data1′);$where_clause = array(
‘column_2′ => ‘data2′);$wpdb->update($table,$data_array,$where_clause);
要从数据库中取数据,也有很多种方法,其中一种如下:
$querystr = select column_1 from test_table;$results = $wpdb->get_results($querystr);$i=0;while ($icolumn_1.
;$i++;}
查询 php语法
query(delete from $wpdb->post where post_id = ’13′ “); ?>
其中query的参数是任何mysql语句。返回值是有多少行被选出、影响。如果出错返回false。
选出一个变量
get_var('query',column_offset,row_offset); ?>
其中query为要查询的mysql语句,如果为空的话,则表示从cache中选出。column_offset和row_offet表示制定query返回值的第几列和第几行,缺省值为零。典型用法为:
get_var($wpdb->prepare(select count(*) from $wpdb->users;));?>
这个sql只选出一个值,缺省的0行0列,即表示选出用户数目。目前还不清楚,这里为什么总是要加prepare在前面。
选出一行
get_row('query', output_type, row_offset); ?>
query为要执行的mysql语句,output_type表示返回值是object,hash或者是数组;row_offset表示第几行。
缺省情况下output_type为object。
$mylink = $wpdb->get_row(select * from $wpdb->links where link_id = 10);
echo $mylink->link_id; // prints 10
如果output_type=array_a,那么:
$mylink = $wpdb->get_row(select * from $wpdb->links where link_id = 10, array_a);echo $mylink['link_id']; // prints 10
选出一列
get_col('query',column_offset); ?>
一般选出
//$wpdb->get_results('query', output_type);get_results(select id, post_title from $wpdb->postswhere post_status = 'draft' and post_author = 5);foreach ($fivesdrafts as $fivesdraft) {echo $fivesdraft->post_title;}
插入一行
//insert( $table, $data, $format ); ?>insert('table', array('column1' => 'value1', 'column2' => 123 ), array('%s','%d') ) ?>
更新
//$wpdb->update( $table, $data, $where, $format = null, $where_format = null );update( 'table', array( 'column1' => 'value1', 'column2' => 'value2' ), array( 'id' => 1 ), array( '%s', '%d' ), array( '%d' ) ) ?>
关于wpdb prepare
前面提到不清楚为什么每个mysql语句都会包在prepare中,这里给出解释:因为mysql语句中可能含有单引号双引号这样的字符,如果不加 处理直接送给mysql,可能会导致错误。于是这里通过一个prepare来对mysql语句进行预处理。prepare的语法是:
$sql = $wpdb->prepare( 'query' [, value_parameter, value_parameter ... ] );
在query中可以包含%d,%s,%f,分别表示后面参数的类型是整数,字符和浮点,如果要显示%号,则用%%,语法和c语言里面的printf基本一样。
到这里基本上就讲完了。对一般数据库的处理应该都没有问题了。如果碰到问题可以在本文开始时提到的那篇文章中去查阅。
$wpdb是一个全局变量,包含多个关于数据库查询函数:
$wpdb -> get_results('query');$wpdb->query('query');$wpdb->get_var('query',column_offset,row_offset);$wpdb->get_row('query', output_type, row_offset);$wpdb->get_col('query',column_offset);$wpdb->get_results('query', output_type);$wpdb->insert( $table, $data, $format );$wpdb->update( $table, $data, $where, $format = null, $where_format = null );$wpdb->prepare( 'query' [, value_parameter, value_parameter ... ] );$wpdb->show_errors();$wpdb->hide_errors();$wpdb->print_error();$wpdb->get_col_info('type', offset);$wpdb->flush();
更多:http://codex.wordpress.org/zh-cn:class_reference/wpdb
以上就介绍了wordpress 数据库操作用的函数。,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。