Probably been covered but I keep getting “connecting to the WordPress database” which isn’t what I’m after. In a nutshell I have a perfectly fine WordPress site, I also have a MySQL database on another server which has some data I’m after. The code works fine on phpfiddle (is this the standard?) but whenever I try to use it on the WordPress site, either with shortcodes or in the header.php it keeps hanging, throwing errors and won’t connect to the DB, any ideas why?
Edit: To make things clearer, it’s a meteorological database I want to connect to in order to pull things like Temperature from. Nothing native or related to WordPress.
<?php
// Create connection
$con=mysqli_connect("ip","user","pass","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($con);
?>
Gives me this
Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on...
You should probably be looking at the WordPress WPDB class – https://codex.wordpress.org/Class_Reference/wpdb
For example:
To connect to a second (and third, and forth…) database, I recommend HyperDB plugin.