0
votes

I am trying to do some coding and I've taken a free webhost for that.

$servername = "127.0.0.1";
$username = "ltm_23041946";
$password = "pass_wass_heree";
$dbname = "ltm_23041946_a";
$conn = new mysqli($servername, $username, $password, $dbname);

Is my code, but I get multiple errors with this. Here are my errors;

Warning: mysqli::__construct(): (HY000/2002): Connection refused in >/home/vol14_2/ultimatefreehost.in/ltm_23041946/htdocs/create.php on line 12

Notice: Undefined variable: conn in >/home/vol14_2/ultimatefreehost.in/ltm_23041946/htdocs/create.php on line 18

Fatal error: Uncaught Error: Call to a member function query() on null in >/home/vol14_2/ultimatefreehost.in/ltm_23041946/htdocs/create.php:18 Stack >trace: #0 /home/vol14_2/ultimatefreehost.in/ltm_23041946/htdocs/create.php(51): >insert('a', 'md2') #1 {main} thrown in >/home/vol14_2/ultimatefreehost.in/ltm_23041946/htdocs/create.php on line 18

I searched around trying to get answers, but I am still stuck with this. If anyone could help, I would be thankfull!

1
Which is line 18 in this script of 5 lines please?RiggsFolly
The first warning seems to be the cause of the rest of the problems; you don't have a database connection.jeroen
Did you start your MySQL Server? normally connection refused means you didnt, or it failed to start #RiggsFolly
@RiggsFolly $conn->query($sql); is line 18minecraft account
Have you checked if the ip of your mysql server is actually localhost or something else? Usually shared hosting providers have a different url / ip for their sql databases.Alberto

1 Answers

0
votes

Even using 127.0.0.1 connection might not work in servers with multiple interfaces or with special routing rules. Run mysqlcheck --help and take the IP address and port shown at the bottom in the "port" and "host" fields. For example:

port                              3306
host                              192.168.1.23

Use the IP and port that MySQL recognizes:

$servername = "192.168.1.23:3306";