I’m working on coding a custom plugin which needs to query data from a simple table I created via WP Data Access using CSV upload. When I query the WordPress database, I receive the following error:
WordPress database error Table ‘lthshuya_wp893.productlookup’ doesn’t exist for query SELECT * FROM productlookup WHERE competitor_brand = ‘Hu Friedy’ AND competitor_part LIKE ‘%678-210%’
The database is lthshuya_wp224, but when I tried putting that before the table name, I got this error:
WordPress database error SELECT command denied to user ‘lthshuya_wp893’@’localhost’ for table lthshuya_wp224.productlookup for query SELECT nordent_part FROM lthshuya_wp224.productlookup WHERE competitor_brand = ‘Hu Friedy’ AND competitor_part LIKE ‘%678-210%’
I used the global $wpdb; to access the database so I’m not sure why there would be a permissions error. Could you please provide some direction as to how I can query the productlookup table?
Thank you!!!
The page I need help with: [log in to see the link]
Yes, I can access and manage lthshuya_wp224 and the productlookup table I created from the WP admin section just fine. The issue is that my PHP code doesn’t seem to want to recognize that table.
This is the code I had been using:
global $wpdb;
$query = "SELECT nordent_part FROM productlookup WHERE competitor_brand = '" . $name . "' AND competitor_part LIKE '%" . $part . "%'";
$results = $wpdb->get_results( $query );
Based on the example you sent, I replaced it with the following:
$wpdadb = WPDataAccess\Connection\WPDADB::get_db_connection( $wpdb );
$query = "SELECT nordent_part FROM productlookup WHERE competitor_brand = '" . $name . "' AND competitor_part LIKE '%" . $part . "%'";
$results = $wpdadb->get_results( $query );
Which then resulted in this error:
[30-Apr-2024 00:50:05 UTC] PHP Fatal error: Uncaught Error: Class 'WPDataAccess\Connection\WPDADB' not found in /home/lthshuya/dev.maryschieferstein.com/test-site/wp-content/plugins/nordent-product-lookup-tool/core/includes/nordent-product-lookup-tool-action.php:20
Stack trace:
#0 {main}
thrown in /home/lthshuya/dev.maryschieferstein.com/test-site/wp-content/plugins/nordent-product-lookup-tool/core/includes/nordent-product-lookup-tool-action.php on line 20
Can you please tell me what I need to do to connect properly so that I can access the table? I didn’t expect to run into this error, and apparently my client needs this working this week.
No, it gives the same error that it doesn’t have the ‘WPDataAccess\Connection\WPDADB’ class. Is there a file I need to include in order to get that class? This is in a subfolder of a plugin, so it seems that things aren’t being included automatically.
Would it be more helpful if I could give you access to take a look?
[01-May-2024 10:19:20 UTC] PHP Fatal error: Uncaught Error: Class 'WPDataAccess\Connection\WPDADB' not found in /home/lthshuya/dev.maryschieferstein.com/test-site/wp-content/plugins/nordent-product-lookup-tool/core/includes/nordent-product-lookup-tool-action.php:20
Stack trace:
#0 {main}
thrown in /home/lthshuya/dev.maryschieferstein.com/test-site/wp-content/plugins/nordent-product-lookup-tool/core/includes/nordent-product-lookup-tool-action.php on line 20
My client needs this plugin working this week, and there’s more I’ll have to do once I get the database connection working. I’ve used WP Data Access previously and was able to query tables via $wpdb, so I wasn’t expecting to run into all these problems. Is there any way we could troubleshoot this more quickly?
[01-May-2024 10:44:08 UTC] WordPress database error SELECT command denied to user 'lthshuya_wp893'@'localhost' for table lthshuya_wp224.productlookup for query SELECT nordent_part FROM lthshuya_wp224.productlookup WHERE competitor_brand = 'Hu Friedy' AND competitor_part LIKE '%678-210%';
It seems that it’s connecting to the database with a different user than the one defined in wp-config.php. Is there a way to edit the WPDADB connection to fix that?
It looks like your lookup table is stored in another database (= not your WordPress database) and your WordPress user has no access to that table. You have two options to fix this:
(1) Create a remote connection and connect as the owner (2) Give your WordPress user select access to the table
BTW, importing WP Data Access files should not be necessary. Please fix this code before your continue: $wpdadb = \WPDataAccess\Connection\WPDADB::get_db_connection(‘lthshuya_wp224’);
For this code to work you have to wait until all plugins are loaded! (see hook: plugins_loaded)
The WordPress user defined in the config file is lthshuya_wp224, not lthshuya_wp893, so it’s connecting to the database with the wrong user. If I can switch it to lthshuya_wp224 it should work.
The table is set up within the WordPress database:
If I set up a new wpdb object with explicit connection information, it runs the query:
$wpdb_npl = new wpdb(/* connection information here */);
$query = "SELECT nordent_part FROM productlookup WHERE competitor_brand = '" . $name . "' AND competitor_part LIKE '%" . $part . "%';";
$results = $wpdb_npl->get_results( $query );
But $results seems to always be empty. When I run the same query in WP Data Access’ SQL interface, I get a result. Is that because it has to be set up as a WPDADB object?
If your table is stored in your WordPress database, you don’t need the WPDBA class. In that case you can use $wpdb to connect. Your initial code should work:
global $wpdb;
$query = "SELECT nordent_part FROM productlookup WHERE competitor_brand = '" . $name . "' AND competitor_part LIKE '%" . $part . "%'";
$results = $wpdb->get_results( $query );
You only need WPDADB to connect to remote and local non WordPress databases.
If you get an access denied error, this is probably an authorization issue. To fix it, make sure your WordPress user is allowed to access your table.
I finally figured out the issue. I set up my test WordPress site in a subdirectory of another WordPress site. No matter what I did, the database connection kept trying to use the information for the site in the main directory rather than for the one in the subdirectory. I set up a new test site at its own subdomain and then everything worked. Never would have expected that to cause an issue, especially when I was pulling in the correct wp-config information.
Thank you so, so much for all your help!!! Sorry it turned out to be a weird, unrelated problem.