简体   繁体   中英

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.

I'm useing this code. Error is in this line:

$mysqli = new mysqli("hostex.lt","myInfo","pass","myInfoo");

"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:

This code working in other project where I use localhost, so here is problem.

        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.

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.

If you don't have access to the Privileges section in your PHPMyAdmin you will have to contact the hoster of your database.

This is unlikely to be a PHP problem per se , but most probably something on the database server that is blocking the connection.

Try the following in this order, running your code after each step:

New MySQL user

  • Add a new database user via PHPMyAdmin.
  • Go to the Privileges and then add user.
  • The Host can be set to a '%' (wildcard - allows all servers access. This is less secure but can be tightened up later).
  • Fill in a username and password which you will then use in the PHP code.
  • Retry your code.

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).

  • Comment out the sections 'bind-address = 127.0.0.1' and 'skip-networking' (if its there.) using a # character at the start of the line.
  • Save the file
  • Restart the MySQL service (service mysql restart).
  • Retry your code

Firewall

If the above steps haven't worked, then most likely a firewall is blocking the connection.

  • On the server, allow port 3306 to accept incoming connections
  • If there is a shared firewall device or the cloud hosting partner (eg. AWS) has a firewall, allow port 3306
  • Retry your code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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