$wpdb Class notes

WordPress Logo Symbol
Methods in the wpdb() class should not be called directly. Use the global $wpdb object instead.
$wpdb object can talk to any number of tables but only to one database.(*hyperdb)
Some of the methods in this class require from you to escape all untrusted values you incorporate.
$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 -> replace( $table, $data, $format )
$wpdb -> update( $table, $data, $where, $format=null, $where_format=null )
$wpdb -> delete( $table, $data, $where_format=null )
$wpdb -> query( ‘query’ )
$wpdb -> get_col_info( ‘type’, offset )
$wpdb -> show_errors()
$wpdb -> hide_errors()
$wpdb -> print_error()

$wpdb Methods

$wpdb -> get_var(‘query’, column_offset, row_offset)

query: the sql query
column_offset: default 0
row_offset: default 0

$wpdb -> get_row( ‘query’, output_type, row_offset )

output_type: OBJECT, ARRAY_A, ARRAY_N

$wpdb -> get_col( ‘query’, column_offset )

$wpdb -> get_results( ‘query’, output_type )

output_type:
OBJECT(array of row objects),
OBJECT_K(assoc. array of row objects using first column’s values as keys),
ARRAY_A(numerically indexed array of assoc. arrays),
ARRAY_N(numerically indexed array of num. indexed arrays)

$wpdb -> insert( $table, $data, $format )

$table: the name of the table to insert data into.
$data: scalar data to insert in column=>value pairs. Neither should be SQL escaped.
$format: An array of formats to be mapped to each of the values in $data.If omitted all data is treated as strings, otherwise specified in wpdb::$field_types. Possible values are %s, %d, %f (float).

Returns false if the row could not be inserted, otherwise it returns the number of affected rows.
$wpdb->insert_id returns the ID generated for the AUTO_INCREMENT column

$wpdb -> replace( $table, $data, $format )

$format: (arr|str) If string, that format will be used for all of the values in $data.

Returns false if the length of a string elem. in the $data array is longer than the one defined in the MySql db table.(do not assume data will be truncated)
$wpdb->insert_idreturns a count to indicate the number of rows affected.
1: for a single row replace
>1: one row is inserted and many deleted

$wpdb -> update( $table, $data, $where, $format=null, $where_format=null )

$data: column=>value pairs(neither should be SQL escaped).
$where: assoc. array. Multiple clauses will be joined with ANDs.
$format: array mapped to each of the values in $data string that will be used for all.
$where_format:
array | mapped to each of the values in $where.
string | used for all.

Returns the number of rows updated, or false. If the $data mathes what is allready in the db, no-rows will be updated, so 0 will be returned.

$wpdb -> delete( $table, $data, $where_format=null )

Returns the number of rows deleted, of false on error.

$wpdb -> query( ‘query’ )

for SELECT, INSERT, DELETE, UPDATE etc. returns an integer value indicating the number of rows affected/selected.
for CREATE, ALTER, TRUNCATE, DROP (which affect whole tables instead of specific rows) returns:
$result === true // returns true on success
$result === false // check for errors
$result === 0 // check whether any rows were affected

$wpdb -> get_col_info( ‘type’, offset )

‘type'(str): name(def.), table, max_length, not_null, primary_key, unique_key, numeric, blob, type, unsigned, zerofill
offset(int): Specifies the column from which to retrieve information. Zero(0) for the 1st column. Defaults to -1 which retrives information for all columns and outputs as array.

$wpdb -> show_errors()

$wpdb -> hide_errors()

$wpdb -> print_error()

$wpdb Security

$sql = $wpdb -> prepare( ‘query’, value_parameter[, value_parameter … )

‘query’: The SQL query you wish to execute, with placeholders
value_parameter(s): The value(s) to substitute into the placeholder(s). Alternatyvely can be an array(as in vsprintf())

Placeholders: %s, %d, %f. All % chars inside SQL string literals(including LIKE wildcards) must be double escaped as %%.

$wpdb Properties

$wpdb -> show_errors
$wpdb -> num_queries
$wpdb -> last_query
$wpdb -> last_error
$wpdb -> queries
$wpdb -> last_result
$wpdb -> col_info
$wpdb -> insert_id
$wpdb -> num_rows
$wpdb -> prefix
$wpdb -> base_prefix

$wpdb Tables Referances

$wpdb -> posts
$wpdb -> postmeta
$wpdb -> comments
$wpdb -> commentmeta
$wpdb -> termmeta
$wpdb -> terms
$wpdb -> term_taxonomy
$wpdb -> term_relationships
$wpdb -> users
$wpdb -> usermeta
$wpdb -> links
$wpdb -> options

Referance:

WordPress Codex