WordPress database Operation Wpdb Object ($wpdb) usage detailed "reprint"

Source: Internet
Author: User
Tags prepare 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 '); 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, 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.] <br/> "; $i + +;}

  Inquire

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:

<?php $user _count = $wpdb->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 = ten"); 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"

If Output_type=array_n, then:

$mylink = $wpdb->get_row ("select * from $wpdb->links WHERE link_id = ten", array_n); echo $mylink [1]; Prints "10"

  Select a column

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

  General Election

$wpdb->get_results (' query ', output_type); <?php$fivesdrafts = $wpdb->get_results ("Select ID, Post_title From $wpdb->postswhere post_status = ' draft ' and Post_author = 5 "), foreach ($fivesdrafts as $fivesdraft) {echo $FIVESDR Aft->post_title;}

  Insert a row

<?php $wpdb->insert ($table, $data, $format);? ><?php $wpdb->insert (' table ', Array (' column1 ' = ') Value1 ', ' column2 ' = + 123), Array ('%s ', '%d '))?>

  Update

1 // $wpdb->update ($table, $data, $where, $format = null, $where _format = null); 2 $wpdb Array Array Array 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:

1 $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:

1 $wpdb-Get_results (' query ');2 $wpdb->query (' query ');3 $wpdb->get_var (' query ', Column_offset,row_offset);4 $wpdb->get_row (' query ', Output_type,row_offset);5 $wpdb->get_col (' query ',column_offset);6 $wpdb->get_results (' query ',output_type);7 $wpdb->insert ($table,$data,$format );8 $wpdb->update ($table,$data,$where,$format=NULL,$where _format=NULL );9 $wpdb->prepare (' query ' [, Value_parameter, Value_parameter ... ] );Ten $wpdb-show_errors (); One $wpdb-hide_errors (); A $wpdb-print_error (); - $wpdb->get_col_info (' type ',offset); - $wpdb-Flush();

  

WordPress database Operation Wpdb Object ($wpdb) usage detailed "reprint"

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.