Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I connect to a db4free.net database with PHP?

Tags:

database

php

I made a db on db4free.net, and I want to connect to it with php:

$username = myusername; 
$password = mypw; 
$host = "db4free.net:3306"; 
$dbname = mydbname; 

$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 

try 
{ 
    $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
} 
catch(PDOException $ex) 
{ 
    die("Failed to connect to the database: " . $ex->getMessage()); 
} 

And I get this error message: Failed to connect to the database: SQLSTATE[HY000] [2005] Unknown MySQL server host

So I was thinking , that the hostname is wrong, so i tried to varie it a bit (adding /dbname or /username or removing the port, ect.). I've tried all the combinations, but none of them worked. The login data are checked multiple times. What could be the problem?

like image 578
RuntimeException Avatar asked Jul 13 '14 14:07

RuntimeException


3 Answers

This may be too late to post an answer to this question.

But I hope this would help anyone who comes searching a solution for a connection failure or access denial just as me.

db4free.net while having database listening on the port 3306, they provide mysql servers on some other ports as well. When creating your account make sure to keep in mind the server you selected.

As in my case, my account was created on their newly set up mysql 8 server and it runs on the port 3307

like image 82
Achala Dissanayake Avatar answered Sep 23 '22 10:09

Achala Dissanayake


This worked for me...

$host = "db4free.net"; 
like image 30
MKRNaqeebi Avatar answered Sep 21 '22 10:09

MKRNaqeebi


I tried using this code and it worked for me

$host = "85.10.205.173:3306"; 
like image 9
Mehran Avatar answered Sep 22 '22 10:09

Mehran