How to define composite keys with dbDelta()

The problem If the table already exists your code will still try to execute the following queries: 1) ALTER TABLE wp_voicemail_call CHANGE COLUMN user_id user_id BIGINT(9) UNSIGNED NOT NULL 2) ALTER TABLE wp_voicemail_call CHANGE COLUMN call_id call_id BIGINT(9) UNSIGNED NOT NULL 3) ALTER TABLE wp_voicemail_call CHANGE COLUMN opened opened BOOL DEFAULT 0 NOT NULL 4) … Read more

Use wpdb->prepare for `order by` column name

You can’t use prepare for column names, and you can’t really use it for the sort order either. prepare will always quote the string. You will need to swap in the values yourself. Rather than try to “sanitize” the data, I’d use a white-list approach. $orderby = array( ‘date’ => ‘post_date’, // etc ); $sortorder … Read more

WordPress Unit Testing – Cannot Create Tables

You’ve just discovered an important feature of the core test suite: it forces any tables created during the test to be temporary tables. If you look in the WP_UnitTestCase::setUp() method you’ll see that it calls a method called start_transaction(). That start_transaction() method starts a MySQL database transaction: function start_transaction() { global $wpdb; $wpdb->query( ‘SET autocommit … Read more

$wpdb->get_results(…) returns empty array despite correct query

Use $wpdb->show_errors( true ) before the query and see what error comes back. My problem (because I’ve experienced the same thing) was that I should use $wpdb->posts instead of wp_posts inside the query. The prefix of the tables can change from WP installation to installation or even in the same installation depending on the time … Read more

Matching database content types to PHP types

There is no way to automatically make wpdb do this. This is really a limitation of PHP that WordPress doesn’t happen to address. wpdb doesn’t have any hooks or actions that would help you either, so the best thing you can do is write your own wrapper function and run your queries through that so … Read more

What is the best way to sanitize data?

No the sanitization is already done. Well the mysql_real_escape_string is done, it’s considered bad form to filter html on input. I personally think doing it on output kinda breaches DRY. If you did in WordPress I highly suspect somewhere else will do it again resulting in double html entities encoding. Also by the way, wpdb::insert … Read more

$wpdb->update multiple rows, like IN in normal SQL

As you can see in source code the = sign is hardcoded in the wpdb::update() method, so, by default, is not possible to use IN for update method. Simplest way to do the trick is to use the wpdb::query() with your sql query, just be sure to properly escape all values Example: function wpdb_update_in( $table, … Read more

Does a query executed through wpdb class get cached?

Nope, doesn’t work like that. The database-related caching is minimal and mostly covers using precisely same queries during single page load. Best way to cache persistently database and/or computationally intensive results is using Transients API to store results for fitting period of time.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)