23
Custom Database Queries in WordPress An overview of the $wpdb object Custom Database Queries in WordPress Topher DeRosia @topher1kenobe

Custom Database Queries in WordPress

Embed Size (px)

Citation preview

Page 1: Custom Database Queries in WordPress

Custom Database Queries in WordPress

An overview of the $wpdb object

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 2: Custom Database Queries in WordPress

Developer and Documenter from

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 3: Custom Database Queries in WordPress

Should I write my own custom queries with WordPress?

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 4: Custom Database Queries in WordPress

NO!WP_Query is efficient and secure.

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 5: Custom Database Queries in WordPress

Should I use custom database tables with WordPress?

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 6: Custom Database Queries in WordPress

NO!WordPress has an excellent database structure that should accommodate nearly any data.

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 7: Custom Database Queries in WordPress

Learn the rules like a pro, so you can break them like an artist” -- Pablo Picasso

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 8: Custom Database Queries in WordPress

Why would you write a custom query?

1. You want something really crazy from WordPress tables.

2. You’re accessing data from custom tables.

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 9: Custom Database Queries in WordPress

Why would you use custom tables?

1. You need to utilize an existing data set.

2. Whatever you’re building is so weird that it can’t use WordPress’ table structure.

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 10: Custom Database Queries in WordPress

How do we talk to MySQL inside WordPress?

The $wpdb object is instantiated very early and provides a number of methods for communicating with the database.

Example:$results = $wpdb->get_results( 'SELECT * FROM wp_options WHERE option_id = 1', OBJECT );

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 11: Custom Database Queries in WordPress

What do we do with a table?

● Select● Insert● Update● Delete

Also some debugging and stats analysis.

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 12: Custom Database Queries in WordPress

SELECT

get_var()$wpdb->get_var( $query, column_offset, row_offset );The get_var function returns a single variable from the database. The entire result of the query is cached for later use. Returns NULL if no result is found.

get_row()$wpdb->get_row($query, output_type, row_offset);Retrieves an entire row. Can return an object, associative array, or numerically indexed array. Caches all matching rows.

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 13: Custom Database Queries in WordPress

SELECT

get_col()$wpdb->get_col( $query, column_offset );Returns a one dimensional array of a column.

get_results()$wpdb->get_results( $query, output_type );Generic, multiple row results can be pulled from the database with get_results. Returns the entire query result as an array.

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 14: Custom Database Queries in WordPress

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

$wpdb->insert( 'table', array(

'column1' => 'value1', 'column2' => 123

), array(

'%s', '%d'

) );

This function returns false if the row could not be inserted. Otherwise, it returns the number of affected rows (which will always be 1).

After insert, the ID generated for the AUTO_INCREMENT column can be accessed with:$wpdb->insert_id

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 15: Custom Database Queries in WordPress

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

$wpdb->update( 'table', array(

'column1' => 'value1', // string'column2' => 'value2' // integer (number)

), array( 'ID' => 1 ), array(

'%s', // value1'%d' // value2

), array( '%d' )

);

Return values: This function returns the number of rows updated, or false if there is an error. Keep in mind that if the $data matches what is already in the database, no rows will be updated, so 0 will be returned. Because of this, you should probably check the return with false === $result

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 16: Custom Database Queries in WordPress

DELETE$wpdb->delete( $table, $where, $where_format = null );

$wpdb->delete( 'table', array( 'ID' => 1 ), array( '%d' ) );

Returns the number of rows updated, or false on error.

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 17: Custom Database Queries in WordPress

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

$wpdb->replace( 'table', array(

'indexed_id' => 1,'column1' => 'value1', 'column2' => 123

), array(

'%d','%s', '%d'

) );

After replace, the ID generated for the AUTO_INCREMENT column can be accessed with:$wpdb->insert_id

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 18: Custom Database Queries in WordPress

query()$wpdb->query( $query );

The query() function allows you to send any query.

IMPORTANT: Returns an integer value indicating the number of rows affected/selected for SELECT, INSERT, DELETE, UPDATE, etc.

For CREATE, ALTER, TRUNCATE and DROP SQL statements, (which affect whole tables instead of specific rows) this function returns TRUE on success.

If a MySQL error is encountered, the function will return FALSE.

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 19: Custom Database Queries in WordPress

Prepared Statements

Prepared statements allow for much greater security.

$wpdb->query( $wpdb->prepare(

"DELETE FROM $wpdb->postmetaWHERE post_id = %dAND meta_key = %s",

13, 'gargle’ )

);

NOTE: $wpdb knows the names of WordPress tables

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 20: Custom Database Queries in WordPress

Prepared Statements

Another example:

$metakey = "Harriet's Adages";$metavalue = "WordPress' database interface is like Sunday Morning: Easy.";

$wpdb->query( $wpdb->prepare( "

INSERT INTO $wpdb->postmeta( post_id, meta_key, meta_value )VALUES ( %d, %s, %s )

", 10,

$metakey, $metavalue

) );

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 21: Custom Database Queries in WordPress

Show and Hide MySQL errors

You can turn error echoing on and off with the show_errors and hide_errors, respectively. <?php $wpdb->show_errors(); ?> <?php $wpdb->hide_errors(); ?>

You can also print the error (if any) generated by the most recent query with print_error. <?php $wpdb->print_error(); ?>

Note: If you are running WordPress Multisite, you must define the DIEONDBERROR constant for database errors to display like so: <?php define( 'DIEONDBERROR', true ); ?>

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 22: Custom Database Queries in WordPress

Important details

● get_results() is the most flexible method, BUT

● Use prepare() whenever possible

● Be consistent

● Read everything on the Codex https://codex.wordpress.org/Class_Reference/wpdb

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

Page 23: Custom Database Queries in WordPress

THANKS FOR

LISTENING

Custom Database Queries in WordPressTopher DeRosia

@topher1kenobe

http://topher1kenobe.comFollow me @topher1kenobe