Home > Article > Backend Development > Functions used for wordpress database operations.
When using wordpress, if you want to directly use the database operation class (wp-db.php) encapsulated in WP, you can use it by including wp-blog-header.php in the code.
define(‘PATH’, dirname(dirname(__FILE__)).‘/’); require_once(PATH . ‘../wp-blog-header.php’); global $wpdb;
When inserting data, one of the methods is to use the insert() function in the wp-db class.
$table = "test_table"; $data_array = array( ‘column_1′ => ‘data1′, ‘column_2′ => ‘data2′ ); $wpdb->insert($table,$data_array);
The first parameter is the name in the database table, and the second parameter is the data to be inserted, which is an array. The name of the key in the array is the column name in the table. In fact, the insert() function also has a third parameter format. Interested friends can check in the method definition of wp-db.php. When updating data, you can use the update() function, for example:
$table = "test_table"; $data_array = array( ‘column_1′ => ‘new_data1′ ); $where_clause = array(
‘column_2′ => ‘data2′ ); $wpdb->update($table,$data_array,$where_clause);
To update data from the database There are many ways to get data, one of which is as follows:
$querystr = "SELECT column_1 FROM test_table"; $results = $wpdb->get_results($querystr); $i=0; while ($i< count($results)){ echo $results[$i]->column_1."<br />"; $i++; }
Query PHP syntax
<strong><?php $wpdb->query("DELETE FROM $wpdb->post WHERE post_id = ’13′ “); ?> </strong>
The parameter of query is any mysql statement. The return value is how many rows were selected and affected. Returns FALSE if an error occurs.
Select a variable
<strong><?php $wpdb->get_var('query',column_offset,row_offset); ?> </strong>
where query is the mysql statement to be queried. If it is empty, it means selecting it from the cache. column_Offset and row_offet indicate the column and row of the query return value, and the default value is zero. Typical usage is:
<?php $user_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users;"));?>
This SQL only selects one value. The default value is 0 rows and 0 columns, which means the number of users is selected. It's unclear why prepare is always added in front here.
Select a row
<strong><?php $wpdb->get_row('query', output_type, row_offset); ?> </strong>
query is the mysql statement to be executed, output_type indicates that the return value is object, hash or array; row_offset indicates the row.
By default, output_type is object.
$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10");
echo $mylink->link_id; // prints "10"
if output_type=ARRAY_A , then:
$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_A); echo $mylink['link_id']; // prints "10"
Select a column
get_col('query',column_offset); ?>
Generally select
//$wpdb->get_results('query', output_type); <?php $fivesdrafts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'draft' AND post_author = 5"); foreach ($fivesdrafts as $fivesdraft) { echo $fivesdraft->post_title; }
Insert a row
<strong>//<?php $wpdb->insert( $table, $data, $format ); ?> <?php $wpdb->insert('table', array('column1' => 'value1', 'column2' => 123 ), array('%s','%d') ) ?></strong>
Update
//$wpdb->update( $table, $data, $where, $format = null, $where_format = null ); <?php $wpdb->update( 'table', array( 'column1' => 'value1', 'column2' => 'value2' ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) ?>
About wpdb prepare
As mentioned earlier, it is not clear why each mysql statement is included in prepare. Here is an explanation: Because the mysql statement may contain characters such as single quotes and double quotes, if it is not processed, it will be sent directly to mysql. , may cause errors. So here we use a prepare to preprocess the mysql statement. The syntax of prepare is:
$sql = $wpdb->prepare( 'query' [, value_parameter, value_parameter ... ] );
The query can contain %d, %S, and %f, which respectively indicate that the type of the following parameters is integer, character, and floating point. If you want to display the % sign, use %%. The syntax is basically the same as printf in C language.
That’s basically it. There should be no problem processing general databases. If you encounter problems, you can check it out in the article mentioned at the beginning of this article.
$wpdb is a global variable that contains multiple database query functions:
$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();
More: http://codex.wordpress.org/zh-cn:Class_Reference/wpdb
The above introduces the functions used in WordPress database operations. , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.