Using wpdb to connect to a different database is not working

I am attempting to use wordpress built in database functionality to connect to a different database on local host. I am doing this in functions.php.

I have no problem achieving this with mysqli, the connection is made and I can test this by printing out the table names inside this database or any other query. However this is not the case with wpdb.

This is working :

$db = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);

This is not working:

$db = new wpdb(DB_USER, DB_PASSWORD, DB_DATABASE, DB_SERVER);

$mytables=$db->get_results(“SHOW TABLES”);
foreach ($mytables as $mytable)
{
foreach ($mytable as $t)
{
echo $t . “<br>”;
}
}

It does not show anything regardless of what query I use, I just get a blank page with a 200 response, indicating nothing is wrong but no data was found. Where as mysqli will show me the data without any problems.

I would just use mysqli but I want to use prepared statements, which does not seem to be an option on wordpress. I need to get that wpdb connectiong working if anyone could give me some clues as to why this does not work.

Read more here:: Using wpdb to connect to a different database is not working

Leave a Reply

Your email address will not be published. Required fields are marked *