Each tab in the Query Builder is connected to a single database at a time. While the USE command allows switching to another database within a query, you cannot maintain simultaneous connections to two databases in the same query. As a result, it’s not possible to directly copy data between databases using standard SQL.
To solve this limitation, WP Data Access introduces two custom plugin commands that enable cross-database data exchange. These are not native MySQL commands — they are available only within the Query Builder.
WPDAVAR (or wpdavar) #
The WPDAVAR command stores the result of the last executed query into a plugin variable. For example, the following code selects all rows from the genre table and saves the result into a variable named my_genres:
select * from genre
/
wpdavar my_genres
/
WPDATMP (or wpdatmp) #
The WPDATMP command creates a temporary table from a variable that was previously set with WPDAVAR. The temporary table will use the same name as the variable:
select * from genre
/
wpdavar my_genres
/
use rdb:my_remote_database
/
wpdatmp my_genres
/
Exchanging data between different databases #
Combining WPDAVAR and WPDATMP allows you to exchange data between two databases. This works for local and remote databases.
Example syncing the genre table from a remote database to WordPress #
-- Select from remote DB
use rdb:my_remote_database
/
select * from genre
/
-- Store results in a temporary table
wpdavar my_genres
/
-- Switch to WordPress DB
use wordpress
/
wpdatmp my_genres
/
-- Insert missing genres
insert into genre select * from my_genres
where genre_name not in (select genre_name from genre)
/
Important #
- Test your query/queries before you schedule
- Make sure you handle all possible exceptions
- A failing query prevents execution of subsequent queries
Limitations #
The synching example temporarily loads the entire dataset into memory. For large tables, this may cause memory issues or execution failure. In such cases, you should split the operation into smaller chunks, using LIMIT/OFFSET or other filtering techniques.
How we can enable multiple registered users update a single database real time? Just like a bidding system. Is it possible with this plug-in? can we implement ajax to show real-time data?
Hi Hardeep,
Multiple logged-in users can update your database tables as long as your server can handle it. If you’re presenting data using the App Builder, there’s a hook you can implement to refresh/reload a table at specific time intervals.
See here: https://wpdataaccess.com/docs/hooks-examples/auto-reload-table-at-specific-intervals/
Hope that helps! If you have more questions, you may send us a message via our contact page.