0

I have one external WordPress file where I'm trying to connect to my WordPress database and another database that's not WordPress all in that one file.

Here's the code in that file:

include('/var/www/html/wp-load.php'); // WP connection

define ("DB_HOST", "localhost");
define ("DB_USER", "mysqluser");
define ("DB_PASS",'password123');
define ("DB_NAME","jobs");
try {
    $dsn = "mysql:dbname=".DB_NAME.";host=".DB_HOST;
    $dbh = new PDO($dsn, DB_USER, DB_PASS);
    return $dbh;
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

Here is the error I get for connecting to the WordPress database:

Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'wordpress'@'localhost' (using password: YES)

How can I fix this?

0

1 Answer 1

4

When you include wp-load.php, WordPress will load wp-config.php, where constants with the same identifiers you have chosen here are already defined. This should be throwing warnings regarding the duplicate definition.

Because you cannot re-define constants, the values you are attempting to set in this file are ignored. Choose different names for them if they are referring to values for your non-WordPress database.

To interact with the WordPress database, it would make more sense to use the WordPress APIs. There's not really a good reason to establish a separate connection when you have already loaded the WordPress environment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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