简体   繁体   English

如何通过php连接其他服务器的mysql数据库

[英]How connect with mysql database in other server by php

my question is about connect with mysql database in other server by php ??我的问题是关于通过 php 连接其他服务器中的 mysql 数据库? Server A is rec.misuratau.edu.ly , Server B is alrand.ly , I need to code pls.服务器 A 是rec.misuratau.edu.ly ,服务器 B 是alrand.ly ,我需要编码。

I am not sure if I understand well.我不确定我是否理解得很好。 You are asking how to make a connection to mysql database on two different server?您是在问如何在两个不同的服务器上连接到 mysql 数据库?

You can do it like this: First create databases on server, create user define password and grant access to the user.您可以这样做:首先在服务器上创建数据库,创建用户定义密码并授予用户访问权限。 Create two separate file: First is Config.php创建两个单独的文件:第一个是 Config.php

define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'http://rec.misuratau.edu.ly'); // replace with servername
define('DB_NAME', 'db');

define('USER_CREATED_SUCCESSFULLY', 0);
define('USER_CREATE_FAILED', 1);
define('USER_ALREADY_EXISTED', 2);

Second is DbConnect.php其次是 DbConnect.php

class DbConnect {

    private $conn;

    function __construct() {        
    }

    /**
     * Establishing database connection
     * @return database connection handler
     */
    function connect() {
        include_once dirname(__FILE__) . '/Config.php';
        // Connecting to mysql database
        $this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

        // Check for database connection error
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        mysqli_set_charset($this->conn,"utf8");
        // returing connection resource
        return $this->conn;
    }

}

LAter you just call the method before sql statements.之后,您只需在 sql 语句之前调用该方法。

Please check the link below as well:请检查以下链接:

https://www.w3schools.com/php/php_mysql_connect.asp https://www.w3schools.com/php/php_mysql_connect.asp

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

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