php - 无法从我的本地主机 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();
}
}
解决方案
推荐阅读
- reactjs - 关于redux saga的导入方法
- python - Web Scraping 没有给出预期的结果
- javascript - 按元素设置视图位置(javascript)
- python - 使用 python pandas 我们如何选择非常特定的行和关联的列
- ios - 在 PJSIP 中启用 G711a 和 G711u 编解码器
- c# - 无法在启动配置时从委托访问 DI 服务?
- javascript - 如何增加属性的值(整数)并在猫鼬中使用一些限制进行更新?
- haskell - 在 haskell 标准输入上处理箭头键 ANSI 转义序列
- python - 可以在 Python ctypes 中需要 Windows 句柄的地方传递整数吗
- wordpress - Wordpress:为没有管理员用户隐藏“每页的项目数”选项