The function used by WordPress database operation.

Source: Internet
Author: User
Tags wordpress database


When using WordPress, if you want to directly use the package of WP in the database operation of the Class (wp-db.php), the wp-blog-header.php included in the code can be used.

Define (' PATH ', DirName (dirname (__file__)). ' /');  Require_once (PATH. ‘.. /wp-blog-header.php ');  

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, the second parameter is the data to be inserted, and 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, and interested friends can use the update () function when updating data in the Wp-db.php method definition, 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 a number of ways to fetch data from a 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 + +;}

querying PHP syntax


  
   

Where the argument to query is any MySQL statement. The return value is how many rows have been selected and affected. Returns False if an error occurs.

Select a variable


  
   


Where query is the MySQL statement to be queried, and if it is empty, it is chosen from the cache. Column_offset and Row_offet indicate the first and the first rows of the query return value, with a default value of zero. Typical uses are:

 
  Get_var ($wpdb->prepare ("Select COUNT (*) from $wpdb->users;"));? >

This SQL selects only one value, the default 0 rows and 0 columns, which means the number of selected users. It is unclear why this is always prepare in front.

Select a row


  
   

Query is the MySQL statement to execute, Output_type indicates that the return value is Object,hash or an array; Row_offset represents the first line.

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 = ten", array_a); Echo $mylink [' link_id ']; Prints "10"

Select a column

Get_col (' query ', column_offset);?>

General Election

$wpdb->get_results (' query ', output_type);
 
   Get_results ("Select ID, post_title from $wpdb->postswhere post_status = ' draft ' and Post_author = 5"); foreach ($FIVESDR Afts 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
It is unclear why each MySQL statement is wrapped in prepare, which is explained here: Because MySQL statements may contain characters that are quoted in single quotation marks, it can cause errors if they are sent directly to MySQL without processing. The MySQL statement is then preprocessed by a prepare. The syntax of prepare is:

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

In query, you can include%d,%s,%f, which indicates that the type of the following argument is an integer, a character and a floating point, and if you want to display the% number, it is the same as the C language.
It's basically finished here. There should be no problem with the processing of the general database. If you encounter a problem, 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:


MORE: http://codex.wordpress.org/zh-cn:Class_Reference/wpdb




The above describes the WordPress database operation function. , including the aspects of the content, want to be interested in PHP tutorial friends helpful.

  • 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.