Sticky Posts
May 18, 2009
Wordpress XMLRPC | rSQL [Remote SQL] Addon
This is the second biggest Wordpress XMLRPC Addon provided with the elWpAPI. It's called rSQL [Remote SQL] and it does exactly what is says. It can run SQL queries at a distance and return results. It allows you to do so very much using SQL directly in the database and, combined with the rFC Addon, can do virtually anything.
It gives you access to most of $wpdb functions: get_var, get_col, get_row, get_results, update, insert, escape, query plus several other non-$wpdb functions as I needed them: get_prefix, get_tables, blog_tables, get_db, get_schemas, get_blogs. There's one very cool function [get_blogs] that will return all blog prefixes existing in all databases to allow you to work into another blog from a distance :)
This is a taste of what rSQL can do
To call 5ub.rSQL you need to this:
- Instantiate a new elWpAPI class $wp = new elWpAPI(xmlrpc, user, pass);
- And call: $wp->doRpcRequestUP('5ub.rSQL', function_name, [param1, param2]);
As you can see it needs the 5ub.rSQL module mentioned, a function name and optional parameters. It can not get easier than this.
<?php
// Get the table prefix
$prefix = $wp->doRpcRequestUP('5ub.rSQL', 'get_prefix');
// Get the default blog tables ... can accept optional arbitrary wp table prefix
$tables = (object)$wp->doRpcRequestUP('5ub.rSQL', 'get_tables', null);
// Get all the tables in the current database starting with prefix
// ... can accept optional arbitrary wp table prefix
$blog_tables = (object)$wp->doRpcRequestUP('5ub.rSQL', 'blog_tables', null);
// Get database name of the blog installation
$schema = $wp->doRpcRequestUP('5ub.rSQL', 'get_db');
// Get databases DB logged-in user can reach and tables in each
$schemas = $wp->doRpcRequestUP('5ub.rSQL', 'get_schemas');
// Get blog prefixes found in each database within current user's reach
$blogs = $wp->doRpcRequestUP('5ub.rSQL', 'get_blogs');
// Count Posts in posts table
$cposts = $wp->doRpcRequestUP('5ub.rSQL', 'get_var', "SELECT COUNT(*) FROM {$tables->posts}");
// Select Post IDs from posts table
$postids = $wp->doRpcRequestUP('5ub.rSQL', 'get_col', "SELECT `ID` FROM {$tables->posts}");
// Select Posts from posts table
$posts = $wp->doRpcRequestUP('5ub.rSQL', 'get_results', "SELECT * FROM {$tables->posts}");
// Select one random post from posts table
$post = $wp->doRpcRequestUP('5ub.rSQL', 'get_row', "SELECT * FROM {$tables->posts} ORDER BY RAND() LIMIT 1");
// Do a random query
$wp->doRpcRequestUP('5ub.rSQL', 'query', "SELECT * FROM {$tables->posts}");
// Insert a new option in options table
$new_option = array( 'blog_id' => 0, 'option_name' => '_xmlrpc_option', 'option_value' => 1 );
$res[] = $wp->doRpcRequestUP('5ub.rSQL', 'insert', $tables->options, $new_option);
// Update inserted option to a new value
$update_where = array( 'blog_id' => 0, 'option_name' => '_xmlrpc_option');
$update_what = array( 'option_value' => 2 );
$res[] = $wp->doRpcRequestUP('5ub.rSQL', 'update', $tables->options, $update_what, $update_where);
// Escape a string under Wordpress database collation
$escaped = $wp->doRpcRequestUP('5ub.rSQL', 'escape', "escape 'this ;)");
?>
rSQL's function are very explicit but I suggest you actually test the non-$wpdb functions and see their output to trully understand them. If you need any function blocked just uncomment the _5ubRPC_Error line I added to the 'sensitive' functions. It is virtually impossible for you to share database with blogs of others but if you do ... this script will give you access to them. Stay legal even if some providers are stupid enough to share a database / database login.
PS: Download the whole thing again. rSQL is updated and wpapi has a spelling error fixed :)


I been using this for a few days and it works smoothly, Great job! For those of us who do not master the wp api, would you care to expand on the “random query” function with another example including a “where” clause, unless it is a wp limitation? I have been trying many things without luck!
First … thanks :)
I think I might know what you ask me about. The ‘query’ function only returns a number representing the modified or returned rows. Use this for anything else but SELECTs.
To retrieve (SELECT) use get_var, get_col, get_row, get_results.
* get_var returns just one value
* get_col returns all values on column
* get_row returns a whole record
* get_results returns all results
Is this it?
Yes, exactly, and I am quite “thick” on this one since is written above it! thanks