0

So I'm trying to connect to an external db in order to access/update information located on a different non-wordpress database (oracle).

I tried constructing a new wpdb object (as suggested by many other posts on stack exchange) like so:

function initialize_rgr_db() {
   global $rgr_db;
   $rgr_db = new wpdb('usr', 'pw', 'sid', 'host');
} 

However, I kept getting the "error connecting to database" when vardumping the $rgr_db object

debug.log displayed the following:

PHP Warning:  mysqli_real_connect(): (HY000/2003): Can't connect to MySQL server on 'host' (111) in /home/dowxx543z3a1/public_html/wp-includes/wp-db.php on line 1612

Alternatively, I tried using oci_connect like so:

$conn = oci_connect('usr', 'pw', '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(Host=host)(Port=1521)))(CONNECT_DATA=(SID=sid)))');
if(!$conn) {
        error_log('DB CONNECTION ERROR NOOO');
        die();
}
...

But then I got call to undefined function.

Based on what I found, it seems like oci8 is not installed in wordpress. Online I found instructions on how to install oci8 on things like xampp or local environments, but nothing related to wordpress and frankly I'm afraid to mess something up.

So, here's my question. If someone could tell me what I'm doing wrong with wpdb, I'd love to find out. My guess is maybe that the oracle sid != the dbname that wpdb requires as the third argument.

Otherwise, could someone offer some guidance as to how to install oci8 (through cpanel)?

Thank you so much.

  • Are you using the correct arguments? The hostname is output as host in the error log. Also, this might be helpful. – Jack Johansson Jun 25 at 13:23
  • I edited it to host. Didn’t want to reveal it – Eli Jun 25 at 13:44
3

You can't use WPDB to connect to databases that aren't MySQL/MariaDB based.

There are no WordPress APIs or WP based solutions that will do this. Instead you will need to use a general PHP solution inside your plugin, and should look at general PHP resources and communities, not WP ones.

I can also guarantee, that this will require additional PHP extensions to be installed, not just PHP code. Doing this will require root access to the server, and will require your host to get involved.

You should consult with stack overflow on how to use that PHP extension, and Serverfault for how to install it, either way you have left the realm of WordPress expertise and knowledge.

As an aside, have you considered setting up a REST API at the other end you can talk to?

  • I feared as much, I just wanted to confirm. Thank you. – Eli Jun 25 at 13:55
  • Also yes, I did consider setting up a REST API, I'm working with someone else on this and he decided he wanted to connect to the db directly. – Eli Jun 25 at 21:04
1

Have you tried the PDO Class yet?

$db = new PDO("sqlsrv:Server=YouAddress;Database=YourDatabase", "Username", "Password");

If that fails, I think you might need specific drivers setup on your WordPress hosting environment to talk to that Oracle DB. (At least to get the PDO class working) https://www.php.net/manual/en/ref.pdo-oci.php

  • 1
    I haven’t tried PDO. I read somewhere that it wasn’t recommended to use pdo over oci, but didn’t look more into it. I’m on a plane right now, I’ll test it when I come back (and will provide the other information you asked for). But yes, the db is open to remote connections. – Eli Jun 25 at 13:47
  • Here is some reading for your plane ride. core.trac.wordpress.org/browser/tags/5.2.1/src/wp-includes/… But WordPress is using mysqli_real_connect() which is not going to work in your instance. @Tom J Nowell is correct above. – ChristopherJones Jun 25 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.