Detecting errors generated by $wpdb->get_results()

There is a class variable that stores the last error string – $wpdb->last_error. By the looks of the way $wpdb is coded, if the query succeeds, $wpdb->last_error will be an empty string, if it fails, it will be the error string returned by MySQL. So something like this would do the trick. $result = $wpdb->get_results(“SELECT … Read more

How to Add an Index to Plugin Database table

You can execute arbitrary SQL statements with wpdb::query(), including Data Definition Statements, e.g. function create_index () { global $wpdb ; $sql = “CREATE INDEX my_index ON {$wpdb->prefix}my_table (my_column)” ; $wpdb->query ($sql) ; return ; } Note: Because $wpdb->query() can execute arbitrary SQL, if the statement you pass to it contains ANY user input, then you … Read more

Get error messages when $wpdb->insert() returns false?

$wpdb->insert() method returns false if the row could not be inserted. Otherwise, it returns the number of affected rows (which will always be 1). 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 … Read more

Is it bad practice to create own table for a plugin?

I rarely disagree with otherwise knowledgeable users, but in this case I can’t help it. In my opinion calling the usage of non-core database tables bad practice per se is just simply wrong. The choice of whether to go with core tables or add your own depends on several factors. A query’s execution time depends … Read more

The MySQL alternatives: Do Percona Server and MariaDB work well with WordPress, and do they make WordPress go better?

I have personally worked with Percona Server and MySQL, not with MariaDB as of yet. Percona provides support for MariaDB, Drizzle, Amazon RDS, and other MySQL products. I learned at Percona Live NYC that Percona gets the latest version of MySQL and injects 30,000 lines of C/C++ that is unique to its Performance Enhancements. MySQL … Read more

DB connection error after copying a WordPress Multisite instance to a second location

One thought – when I go to www.example.com/staging/wp-admin it automatically redirects me to www.example.com/wp-admin Could the redirect from staging.example.com to example.com/staging be conflicting with the existing install? UPDATE: looks like it could be related to .htaccess issues and complicated domain references in the database From the WP Codex: Moving WordPress Multisite Multisite is far more … Read more

Please explain how WordPress works with MySQL character set and collation at a low level

There are two defines in wp-config.php of WordPress website: define(‘DB_CHARSET’, ‘utf8’); define(‘DB_COLLATE’, ”); There are several things which are most commonly misunderstood. Names of constants in those defines, might suggest that they are related to the database itself. They are not. They are related to tables within the database. Database creation is totally independent from … Read more