Connecting with external database (functions.php?)
-
Hi everyone,
I’ve been working on this for a week now and still can’t find the right answer.
My situation:
I’ve got another database (on same hosting as current site, so localhost), which I want to use data of in my theme files.$mydatabase = new mysqli("localhost","my_user","my_password","my_db"); // Check connection if ($mydatabase -> connect_errno) { echo "Failed to connect to MySQL: " . $mydatabase -> connect_error; exit(); } // Perform query if ($result = $mydatabase -> query("SELECT * FROM tableabc WHERE xandy='4'")) { (etc)Above is what is in my single_cpt.php file and that works. But I’ve heard I should not have this part in there:
$mydatabase = new mysqli("localhost","my_user","my_password","my_db");and I should use that part in Functions.php. Now i’ve tried to remove the above line and add the line below to functions.php, but can’t seem to get it to work.function connect_another_db() { global $my_db; $my_db= new wpdb('my_user','my_password','my_db', 'localhost'); } add_action('init', 'connect_another_db');Can someone please help me out, would really appreciate it!
The topic ‘Connecting with external database (functions.php?)’ is closed to new replies.