我有一些问题连接到mysql服务器。我可以使用linux终端进行连接,因此我知道我的主机、端口、用户和密码都可以工作。但是,我不能使用PHP连接。
PHP版本: 5.2.17
服务器版本: 5.5.27-log MySQL Community Server (GPL)
下面是一个测试代码示例:<?php
$link = mysqli_connect('host.com:5306', 'user', 'pass', 'db');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s'n", mysqli_connect_error());
exit();
}
if (!mysqli_query($link, "SET a=1")) {
printf("Errormessage: %s'n", mysqli_error($link));
}
/* close connection */
mysqli_close($link);
?>
给出以下警告:
警告:mysqli_connect() [function。[MySQL -connect]: (HY000/2005):未知MySQL服务器主机'host.com:5306' (1) in/…/test.php on line 3
连接失败:未知MySQL服务器主机'host.com:5306' (1)
有什么好主意吗?
端口号必须是一个单独的参数:
$link = mysqli_connect('host', 'user', 'pass', 'db', 5306);
有什么好主意吗?
确定。一个简单的三步解决任何php函数的问题:
- 打开你喜欢的浏览器
- 在地址栏中键入
php.net/
,后跟问题函数的名称: - 回车
现在您有了函数的描述,可以检查适当的参数列表。
host是mysql服务器的IP,或者"localhost"
mysql函数中的host参数必须为"localhost"或"127.0.0.1"
您将参数设置为"host:port",这是您的错误。它需要是服务器或具有完整IP地址的远程服务器中网络的完整别名。您可以在这里找到有关此错误的更多信息:http://compnetworking.about.com/od/workingwithipaddresses/g/127_0_0_1_def.htm
mysql的书在php.net: http://www.php.net/manual/en/function.mysql-connect.php