Functions used for wordpress database operations.

Source: Internet
Author: User
Tags wordpress database
: This article mainly introduces the functions used for wordpress database operations ., If you are interested in the PHP Tutorial, refer. Zookeeper

When using wordpress, if you want to directly use the class (wp-db.php) for database operations encapsulated in WP, you can include the wp-blog-header.php into 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 of the database table, and the second parameter is the data to be inserted, which is an array. The key in the array is the name of the column in the table. In fact, the insert () function has a third parameter format, interested friends can look at the method definition in the wp-db.php to update the 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);

There are also many methods to retrieve data from the database, 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."
";$i++;}

Query php syntax


  query("DELETE FROM $wpdb->post WHERE post_id = ’13′ “); ?> 

The query parameter is any mysql statement. The returned value is the number of rows selected and affected. If an error occurs, FALSE is returned.

Select a variable


  get_var('query',column_offset,row_offset); ?> 


Query is the mysql statement to be queried. if it is null, query is selected from the cache. Column_Offset and row_offet indicate the number of columns and the number of rows returned by the query. the default value is zero. Typical usage:

 get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users;"));?>

This SQL statement selects only one value. The default value is 0 rows and 0 columns, indicating the number of users selected. It is not clear why prepare is always added here.

Select a row


  get_row('query', output_type, row_offset); ?> 

Query is the mysql statement to be executed. output_type indicates that the returned value is an object, hash, or array. row_offset indicates the row number.

By default, output_type is an 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 selected

//$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 a row

//
  insert( $table, $data, $format ); ?>
  insert('table', array('column1' => 'value1', 'column2' => 123 ), array('%s','%d') ) ?>


Update

//$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' ) ) ?>

About wpdb prepare
As mentioned above, it is unclear why every mysql statement is packaged in prepare. here we will explain that mysql statements may contain characters such as single quotation marks and double quotation marks. if they are not processed, they will be directly sent to mysql, errors may occur. Therefore, a prepare is used to pre-process mysql statements. The prepare syntax is:

$sql = $wpdb->prepare( 'query' [, value_parameter, value_parameter ... ] );

The query can contain % d, % S, and % f, which indicate the following parameter types: integer, character, and floating point. to display the % sign, use %, the syntax is basically the same as that of printf in c.
This is basically the end of the lecture. There should be no problem with the processing of general databases. If you encounter any problems, you can refer to 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 describes the functions used for wordpress database operations ., Including some content, hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.