Cannot connect to MySQLi?

January 2, 2017 1.1k views
MySQL DigitalOcean PHP
newvoids
By:
newvoids

Hello DigitalOcean community,
I've finally hosted my php files on my webhost, but now I've ran into an issue with MySQLi. When I try to connect to it, I get an error. Below is the code I'm using to connect:

$Connect = new mysqli("http://159.203.111.##/", "root", "########", "log_db");
if ($Connect->connect_error) {
     die("An error has occurred while connecting to the host");
}

This always returns "An error has occured while connecting to the host". I can also connect to "159.203.111.##/phpmyadmin" with those passwords aswell. I'm using WAMP and PHP to do this.

Does anyone know the issue?

1 Answer

Code looks like you are trying to connect over http to your database. This is probably root of issue, try removing it.
After removing code will be something along:

$Connect = new mysqli("159.203.111.##", "root", "########", "log_db");
if ($Connect->connect_error) {
     die("An error has occurred while connecting to the host");
}

If issue still persist, change IP address to localhost if database is on same server:

$Connect = new mysqli("localhost", "root", "########", "log_db");
if ($Connect->connect_error) {
     die("An error has occurred while connecting to the host");
}
Have another answer? Share your knowledge.