连接到在线服务器上的mysql数据库

时间:2015-02-12 16:15:35

标签: php mysql

我最近买了一个新的域名及其托管,它带有一个控制面板和mysql数据库。这是我希望与我的网站一起使用的mysql数据库。我已经创建了一个示例数据库和一个.php文件来查询和回显数据,但我在运行php脚本时得到以下错误。我不知道我哪里出错了。

SQLSTATE[HY000] [1129] Host 'hostname' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'

这是数据库连接代码

    <?php
class Database{
private static $dbName = 'mydatabase';
private static $dbHost = 'www.mydomain.co.zw';
private static $dbUsername = 'myusername';
private static $dbPassword = 'mypassword';

private static $cont = null;

public function __construct() {
    die('Init not allowed');
}
public static function connect(){
    //Open connection through the who;e application
    if(null == self::$cont){
        try {
            self::$cont =  new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbPassword); 
        } catch (PDOException $ex) {
            die($ex->getMessage());
        }
    }
    return self::$cont;
}
public static function disconnect(){
    self::$cont == null;
}
}
?>

这是我用来查询数据库表的代码

   <?php
    include 'database.php';
    $pdo = Database::connect();
    $sql = 'SELECT * FROM test_table';
    $q = $pdo->prepare($sql);
    $q->execute(array($sql));
    $array = array();
    while ($row = $q->fetch(PDO::FETCH_ASSOC)){
     array_push($array, $row);
    }
   $json = json_encode($array);
   echo $json;
   Database::disconnect();
?>

1 个答案:

答案 0 :(得分:0)

您是否尝试过将localhost用作dbHost?