webmaster alex l
webmaster alex l

Reputation: 663

Wordpress external DB connection error

I have an external database I am trying to have a php page inside my WP connect to. If I go to the page directly outside of wordpress I see all the content from the DB displayed correctly. If I look at the page inside of WP I get a DB connection error. Anyone know whats going on?

DB connection.php being included through a shortcode on a WP page.

<?php
// set database host
define ("DB_HOST", "www.example.com");
// set database user
define ("DB_USER", "user1");
// set database password
define ("DB_PASS","password1");
// set database name
define ("DB_NAME","databasename");

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not make connection to DataBase: ' . mysql_error());

$db = mysql_select_db(DB_NAME, $link) or die('Could not select database: ' . mysql_error());
?>

Current output is a connection failor but again, when I look at the page in my browser outside of the WP page the connection is fine and all content is displayed.

Upvotes: 0

Views: 2021

Answers (1)

mikaint
mikaint

Reputation: 353

for external connections i use the following code in template's folder functions.php

$wpdbNew = new wpdb('username', 'password', 'database', 'host');
$wpdbNew->show_errors();

and then you can use $wpdbNew for all your queries, like werdpress' default $wpdb.

Upvotes: 1

Related Questions