天天看點

使用Wordpress中的wpdb類操作資料庫

<code>$wpdb</code>對象可以用來操作WordPress資料庫中的每一個表,不僅僅是WordPress自動建立的基本表。例如,你有一個自定義的表叫做mytable,那麼可以使用如下語句來查詢: <code></code>

這個查詢函數允許你在wordpress的資料庫裡運作任何SQL查詢。當然了,最好能利用如下的特定函數,

<code> &lt;?php $wpdb-&gt;query('query'); ?&gt; </code>

<dl></dl>

<dt>query </dt>

<dd>(string) 你需要執行的SQL查詢</dd>

此函數傳回操作/查詢的行或列的整數。如果出現了MySQL錯誤,此函數将傳回 <code>FALSE</code>(注意: 因為 0 和 FALSE 都可能被傳回, 確定你使用了正确的比較運算符:等于 <code>==</code> vs. 一緻 <code>===</code>)。

删除屬于id為13的文章的‘gargle’meta 鍵和值。

The <code>get_var</code> function returns a single variable from the database. Though only one variable is returned, the entire result of the query is cached for later use. Returns NULL if no result is found.

<code> &lt;?php $wpdb-&gt;get_var('query',column_offset,row_offset); ?&gt; </code>

<dd>(string) The query you wish to run. Setting this parameter to <code>null</code> will return the specified variable from the cached results of the previous query.</dd>

<dt>column_offset </dt>

<dd>(integer) The desired column (0 being the first). Defaults to 0.</dd>

<dt>row_offset </dt>

<dd>(integer) The desired row (0 being the first). Defaults to 0.</dd>

擷取并顯示使用者數量

To retrieve an entire row from a query, use <code>get_row</code>. The function can return the row as an object, an associative array, or as a numerically indexed array. If more than one row is returned by the query, only the specified row is returned by the function, but all rows are cached for later use. Returns NULL if no result is found.

<code> &lt;?php $wpdb-&gt;get_row('query', output_type, row_offset); ?&gt; </code>

<dd>(string) The query you wish to run.</dd>

<dt>output_type </dt>

<dd>One of three pre-defined constants. Defaults to OBJECT.</dd>

OBJECT - result will be output as an object.

ARRAY_A - result will be output as an associative array.

ARRAY_N - result will be output as a numerically indexed array.

擷取ID為10的連結的全部資訊

<code>$mylink</code>對象的屬性是SQL查詢結果的列名(此例中是所有 <code>$wpdb-&gt;links</code>表中的列名)。

作為對比, 使用

将傳回關聯數組:

然後

将傳回索引數組:

To SELECT a column, use <code>get_col</code>. This function outputs a dimensional array. If more than one column is returned by the query, only the specified column will be returned by the function, but the entire result is cached for later use. Returns an empty array if no result is found.

<code> &lt;?php $wpdb-&gt;get_col('query',column_offset); ?&gt; </code>

<dd>(string) the query you wish to execute. Setting this parameter to <code>null</code> will return the specified column from the cached results of the previous query.</dd>

This example lists all posts that contain a particular custom field, but sorted by the value of a second custom field.

Generic, mulitple row results can be pulled from the database with <code>get_results</code>. The function returns the entire query result as an array. Each element of this array corresponds to one row of the query result and, like <code>get_row</code>, can be an object, an associative array, or a numbered array.

<code> &lt;?php $wpdb-&gt;get_results('query', output_type); ?&gt; </code>

<dd>(string) The query you wish to run. Setting this parameter to <code>null</code> will return the data from the cached results of the previous query.</dd>

OBJECT - result will be output as a numerically indexed array of row objects.

OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).

ARRAY_A - result will be output as an numerically indexed array of associative arrays, using column names as keys.

ARRAY_N - result will be output as a numerically indexed array of numerically indexed arrays.

Since this function uses the '$wpdb-&gt;query()' function all the class variables are properly set. The results count for a 'SELECT' query will be stored in $wpdb-&gt;num_rows.

擷取使用者 5 釋出的草稿的id和标題,并顯示标題。

擷取使用者 5 的所有草稿資訊

插入一行資料到資料表中

<code> &lt;?php $wpdb-&gt;insert( $table, $data, $format ); ?&gt; </code>

<dt>table </dt>

<dd>(string) 插入資料的資料表名稱。</dd>

<dt>data </dt>

<dd>(array) 插入的資料 (為 column =&gt; value 鍵值對). $data columns 和 $data values 都可以是 "raw" 資料 (neither should be SQL escaped).</dd>

<dt>format </dt>

<dd>(array|string) (optional) An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.</dd>

Possible format values: %s as string; %d as decimal number; and %f as float.

After insert, the ID generated for the AUTO_INCREMENT column can be accessed with:

如果不能插入行,此函數傳回false

在一行中插入兩列,第一個值為字元串,第二個為數字:

更新資料庫的記錄。

<code> &lt;?php $wpdb-&gt;update( $table, $data, $where, $format = null, $where_format = null ); ?&gt; </code>

<dd>(string) 要更新的表名稱。</dd>

<dd>(array) 需要更新的資料(使用格式:column =&gt; value)。Both $data columns and $data values should be "raw" (neither should be SQL escaped).</dd>

<dt>where </dt>

<dd>(array) A named array of WHERE clauses (in column =&gt; value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".</dd>

<dd>(array|string) (optional) An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data.</dd>

<dt>where_format </dt>

<dd>(array|string) (optional) An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where.</dd>

Possible format values: %s as string; %d as decimal number and %f as float. If omitted, all values in $where will be treated as strings.

更新ID為1的行,第一列的值為字元串,第二列的值為數組:

<code>&lt;?php $sql = $wpdb-&gt;prepare( 'query' [, value_parameter, value_parameter ... ] ); ?&gt;</code>

<dd>(string) The SQL query you wish to execute, with <code>%s</code> and <code>%d</code> placeholders. Any other <code>%</code> characters may cause parsing errors unless they are escaped. All <code>%</code> characters inside SQL string literals, including LIKE wildcards, must be double-% escaped as <code>%%</code>.</dd>

<dt>value_parameter </dt>

Add Meta key =&gt; value pair "Harriet's Adages" =&gt; "WordPress' database interface is like Sunday Morning: Easy." to Post 10.

Note that in this example we pack the values together in an array. This can be useful when we don't know the number of arguments we need to pass until runtime.

Notice that you do not have to worry about quoting strings. Instead of passing the variables directly into the SQL query, use a <code>%s</code> placeholder for strings and a <code>%d</code> placedolder for integers. You can pass as many values as you like, each as a new parameter in the <code>prepare()</code> method.

You can turn error echoing on and off with the <code>show_errors</code> and <code>hide_errors</code>, respectively.

<code> &lt;?php $wpdb-&gt;show_errors(); ?&gt;   &lt;?php $wpdb-&gt;hide_errors(); ?&gt; </code>

You can also print the error (if any) generated by the most recent query with <code>print_error</code>.

<code> &lt;?php $wpdb-&gt;print_error(); ?&gt; </code>

You can retrieve information about the columns of the most recent query result with <code>get_col_info</code>. This can be useful when a function has returned an OBJECT whose properties you don't know. The function will output the desired information from the specified column, or an array with information on all columns from the query result if no column is specified.

<code> &lt;?php $wpdb-&gt;get_col_info('type', offset); ?&gt; </code>

<dt>type </dt>

name - column name. Default.

table - name of the table the column belongs to

max_length - maximum length of the column

not_null - 1 if the column cannot be NULL

primary_key - 1 if the column is a primary key

unique_key - 1 if the column is a unique key

multiple_key - 1 if the column is a non-unique key

numeric - 1 if the column is numeric

blob - 1 if the column is a BLOB

type - the type of the column

unsigned - 1 if the column is unsigned

zerofill - 1 if the column is zero-filled

<dt>offset </dt>

<dd>(integer) Specify the column from which to retrieve information (with 0 being the first column). Defaults to-1.</dd>

-1 - Retrieve information from all columns. Output as array. Default.

Non-negative integer - Retrieve information from specified column (0 being the first).

使用 <code>flush</code> 清除SQL查詢結果緩存

<code> &lt;?php $wpdb-&gt;flush(); ?&gt; </code>

可以清除 <code>$wpdb-&gt;last_result</code>, <code>$wpdb-&gt;last_query</code>, 和 <code>$wpdb-&gt;col_info</code>的緩存。

<dt>$show_errors </dt>

<dt>$num_queries </dt>

<dd>已執行的查詢的數量</dd>

<dt>$last_query </dt>

<dd>已執行的最後一條查詢</dd>

<dt>$queries </dt>

<dd>You may save all of the queries run on the database and their stop times by setting the SAVEQUERIES constant to TRUE (this constant defaults to FALSE). If SAVEQUERIES is TRUE, your queries will be stored in this variable as an array.</dd>

<dt>$last_result </dt>

<dd>最近的查詢結果</dd>

<dt>$col_info </dt>

<dt>$insert_id </dt>

<dd>ID自動增長列生成的最近一條插入語句的ID</dd>

<dt>$num_rows </dt>

<dd>最近一個查詢傳回的行數</dd>

<dt>$prefix </dt>

<dt>       表字首</dt>

<dt></dt>

<dt>$last_error</dt>

<dt>      錯誤資訊</dt>

如果你正在使用多站點, 你也可以通路:

<dt>$blogid </dt>

<dd>部落格ID(多blog環境)</dd>

The WordPress database tables are easily referenced in the <code>wpdb</code> class.

<dt>$posts </dt>

<dd>文章表</dd>

<dt>$postmeta </dt>

<dt>$comments </dt>

<dd>評論表</dd>

<dt>$commentmeta </dt>

<dd>The table contains additional comment information.</dd>

<dt>$terms </dt>

<dt>$term_taxonomy </dt>

<dt>$term_relationships </dt>

<dt>$users </dt>

<dd>使用者表</dd>

<dt>$usermeta </dt>

<dt>$links </dt>

<dd>連結表</dd>

<dt>$options </dt>

<dd></dd>

<dd>本文轉自黃聰部落格園部落格,原文連結:http://www.cnblogs.com/huangcong/archive/2011/07/12/2104398.html,如需轉載請自行聯系原作者</dd>