简体   繁体   中英

How connect with mysql database in other server by php

my question is about connect with mysql database in other server by php ?? Server A is rec.misuratau.edu.ly , Server B is alrand.ly , I need to code pls.

I am not sure if I understand well. You are asking how to make a connection to mysql database on two different server?

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

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

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.

Please check the link below as well:

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

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