首页 > 解决方案 > 无法从我的本地主机 xampp 访问远程 MySQL

问题描述

我正在尝试从 Hostgator 连接到远程 MySQL,但它给了我一个错误,提示Connection failed: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.我已经将我的 IP 添加到 Hostgator 中的访问主机,但仍然没有运气,我应该做任何配置,我目前正在使用Windows 10 机器上的 xampp v3.2.4。

这是配置代码:

      class Database {
      private $host = myip;
    private $user = DB_USERNAME_FROM_Hostgator;
    private $pass = DB_PASS_FROM_Hostgator;
      private $dbname = DB_NAME_FROM_Hostgator;
    
       private $dbh;
    private $error;
    private $stmt;
    
    public function __construct() {
        // Set DSN
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
        $options = array (
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 
        );

        // Create a new PDO instanace
        try {
            $this->dbh = new PDO ($dsn, $this->user, $this->pass, $options);
        }       // Catch any errors
        catch ( PDOException $e ) {
            $this->error = $e->getMessage();
        }
    }
    
    // Prepare statement with query
    public function query($query) {
        $this->stmt = $this->dbh->prepare($query);
    }
    
    // Bind values
    public function bind($param, $value, $type = null) {
        if (is_null ($type)) {
            switch (true) {
                case is_int ($value) :
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool ($value) :
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null ($value) :
                    $type = PDO::PARAM_NULL;
                    break;
                default :
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }
    
    // Execute the prepared statement
    public function execute(){
        return $this->stmt->execute();
    }
    
    // Get result set as array of objects
    public function resultset(){
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_OBJ);
    }
    
    // Get single record as object
    public function single(){
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_OBJ);
    }
    
    // Get record row count
    public function rowCount(){
        return $this->stmt->rowCount();
    }
    
    // Returns the last inserted ID
    public function lastInsertId(){
        return $this->dbh->lastInsertId();
    }
}





标签: phpmysqlxamppremote-server

解决方案


推荐阅读