[英]How to connect to MySQL database from other host using PHP
I need to connect to MySQL using PHP, but database is on a other host , I can't do It correctly. 我需要使用PHP连接到MySQL,但是数据库在另一台主机上,我无法正确执行。
I'm useing this code. 我正在使用此代码。 Error is in this line:
错误在这一行:
$mysqli = new mysqli("hostex.lt","myInfo","pass","myInfoo");
"hostex.lt". “ hostex.lt”。 Database is in hostex.lt, phpMyAdmin: myadmin.hostex.lt I've tried It but It always print
error:error:error:error:error:error:error:error:error:error:
数据库在hostex.lt中,phpMyAdmin:myadmin.hostex.lt我已经尝试过,但它总是打印
error:error:error:error:error:error:error:error:error:error:
This code working in other project where I use localhost, so here is problem. 此代码在我使用localhost的其他项目中有效,因此这里是问题。
for($i=1;$i<=10;$i++)
{
if ($stmt = $mysqli->prepare("INSERT into game (taskai, userName) VALUE (?,?) ")) {
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
// printf(" ", $mysqli->character_set_name());
}
$stmt->bind_param('ss', $score, $name);
// $stmt->bind_param("s", $username);
$stmt->execute();
if ($stmt->error != '') {
echo ' error:'.$stmt->error;
} else {
echo 'success';
}
$stmt->close();
} else {
echo 'error:'.$mysqli->error;
}
}
$mysqli->close();
?>
UPDATE 更新
I've got this error: 我有这个错误:
By default MySQL only allows access from localhost, if you want to access it from a different host you have to allow it explicitly. 默认情况下,MySQL只允许从本地主机访问,如果要从其他主机访问它,则必须明确允许它。
Log into PHPMyAdmin, go to Privileges, add a user 'myDbName'@'www.ftpDomainName.lt' (the host part after the @ is important) and give it access to your database. 登录到PHPMyAdmin,转到Privileges,添加用户'myDbName'@'www.ftpDomainName.lt'(@后的主机部分很重要),并授予其对数据库的访问权限。
If you don't have access to the Privileges section in your PHPMyAdmin you will have to contact the hoster of your database. 如果您无权访问PHPMyAdmin中的“特权”部分,则必须联系数据库的托管人。
This is unlikely to be a PHP problem per se , but most probably something on the database server that is blocking the connection. 本质上 ,这不太可能是PHP问题,但很可能是数据库服务器上的某些问题阻止了连接。
Try the following in this order, running your code after each step: 按此顺序尝试以下操作,在每个步骤之后运行代码:
New MySQL user 新的MySQL用户
Database Server 数据库服务器
On the database server ask the sysadmin to edit /etc/mysql/my.cnf (assumption is that this is a Linux machine with the my.cnf stored at this location -- your sysadmin will know how to find the correct file). 在数据库服务器上,请系统管理员编辑/etc/mysql/my.cnf(假设这是一台Linux计算机,其my.cnf存储在此位置-系统管理员将知道如何查找正确的文件)。
Firewall 防火墙功能
If the above steps haven't worked, then most likely a firewall is blocking the connection. 如果上述步骤不起作用,则很可能是防火墙阻止了该连接。
Note that if the sysadmin of the MySQL server does not want to open the port on the firewall or enable external connections to the database, there is not a lot you can do 请注意,如果MySQL服务器的sysadmin不想打开防火墙上的端口或启用与数据库的外部连接,则您无能为力
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.