如何使用 PHP 连接到 db4free.net 数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24723625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I connect to a db4free.net database with PHP?
提问by RuntimeException
I made a db on db4free.net, and I want to connect to it with php:
我在 db4free.net 上创建了一个数据库,我想用 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.). I've tried all the combinations, but none of them worked. The login data are checked multiple times. What could be the problem?
所以我在想,主机名是错误的,所以我试图改变它(添加 /dbname 或 /username 或删除端口等)。我尝试了所有的组合,但没有一个奏效。多次检查登录数据。可能是什么问题呢?
回答by Mehran
I tried using this code and it worked for me
我尝试使用此代码,它对我有用
$host = "85.10.205.173:3306";
回答by MKRNaqeebi
This worked for me...
这对我有用...
$host = "db4free.net";
回答by Achala Dissanayake
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.
db4free.net 在让数据库侦听端口 3306 的同时,也在其他一些端口上提供 mysql 服务器。创建帐户时,请务必记住您选择的服务器。
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 上运行
回答by Thomas Dutrion
Don't set the port at the end of the hostname. 3306 is indeed the default port for mysql connection anyway.
不要在主机名的末尾设置端口。无论如何,3306 确实是 mysql 连接的默认端口。