• Resolved mary7678

    (@mary7678)


    Hello!

    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]

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Contributor Kim L

    (@kimmyx)

    Hi @mary7678,

    Thanks for posting and we’re sorry to hear you encountered issues with accessing your database.

    Is lthshuya_wp224 accessible for your user in WP Data Access > Data Explorer?

    A sample of how you can query a table via PHP is outlined here: Remote database access | WP Data Access

    You can also send us a message at Contact | WP Data Access if you want to share more information privately.

    We’ll wait for your response!

    Thread Starter mary7678

    (@mary7678)

    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.

    Plugin Contributor Kim L

    (@kimmyx)

    Hi @mary7678,

    Thanks for the response.

    Could you try the following instead?

    $wpdadb = WPDataAccess\Connection\WPDADB::get_db_connection('lthshuya_wp224');
    	$query = "SELECT nordent_part FROM productlookup WHERE competitor_brand = '" . $name . "' AND competitor_part LIKE '%" . $part . "%'";
    	$results = $wpdadb->get_results( $query );

    Let us know if that works. ??

    Thread Starter mary7678

    (@mary7678)

    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?

    Plugin Contributor Kim L

    (@kimmyx)

    Hi @mary7678,

    Thanks for the response.

    It might just be a matter of a missing backslash according to Peter.

    Instead if writing this:
    $wpdadb = WPDataAccess\Connection\WPDADB::get_db_connection('lthshuya_wp224');

    Please try:
    $wpdadb = \WPDataAccess\Connection\WPDADB::get_db_connection('lthshuya_wp224');

    It depends on from where this code is called.

    Let us know how it goes. ??

    Thread Starter mary7678

    (@mary7678)

    I updated the line to:

    $wpdadb = \WPDataAccess\Connection\WPDADB::get_db_connection('lthshuya_wp224');

    And got the same error:

    [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?

    Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    Hi @mary7678

    Just checking: Is WP Data Access installed and activated?

    Thanks,
    Peter

    Thread Starter mary7678

    (@mary7678)

    Yes, it is.

    I managed to get past some of the errors by including these files:

    
    	require_once('../../../wp-data-access/WPDataAccess/Connection/WPDADB.php');
    	require_once('../../../wp-data-access/WPDataAccess/Data_Dictionary/WPDA_Dictionary_Exist.php');

    Now I’m back to getting the following error:

    [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?

    Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    Hi @mary7678

    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)

    Hope this helps,
    Peter

    Thread Starter mary7678

    (@mary7678)

    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:

    Thread Starter mary7678

    (@mary7678)

    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?

    Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    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.

    Does this help?

    Thread Starter mary7678

    (@mary7678)

    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.

    • This reply was modified 6 months, 3 weeks ago by mary7678.
    Plugin Contributor Kim L

    (@kimmyx)

    Hi @mary7678,

    We’re glad to know you were able to find out the root cause of the issue eventually. ??

    Feel free to create a thread or send us a message if you have new questions.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘WP Data Access Table Doesn’t Exist’ is closed to new replies.