简体   繁体   English

如何使用 PHP 连接到 db4free.net 数据库?

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

I made a db on db4free.net, and I want to connect to it with php:我在 db4free.net 上做了一个 db,我想用 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我收到此错误消息:无法连接到数据库:SQLSTATE[HY000] [2005] 未知的 MySQL 服务器主机

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.).所以我在想,主机名是错误的,所以我试图改变它(添加 /dbname 或 /username 或删除端口等)。 I've tried all the combinations, but none of them worked.我尝试了所有的组合,但没有一个奏效。 The login data are checked multiple times.多次检查登录数据。 What could be the problem?可能是什么问题呢?

我尝试使用这个代码,它对我有用

$host = "85.10.205.173:3306"; 

这对我有用......

$host = "db4free.net"; 

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. db4free.net在端口3306上监听数据库时,它们也在其他一些端口上提供mysql服务器。 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 在我的情况下,我的帐户是在他们新设置的mysql 8服务器上创建的,它在端口3307上运行

host = "db4free.net" 
mysql = "8.0.16"
port = "3306"

Don't set the port at the end of the hostname. 不要将端口设置在主机名的末尾。 3306 is indeed the default port for mysql connection anyway. 无论如何,3306确实是mysql连接的默认端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM