我在连接到mysql服务器时遇到问题。我通过Windows的Web安装程序平台安装了PHP快速cgi和iis 7。在我试图连接mysql服务器之前,一切都还可以,它总是失败。这是我的代码:
class MySqlDatabase{
private $mysql;
private $host;
private $username;
private $password;
private $db_name;
function __construct($host, $username, $password, $db_name){
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->db_name = $db_name;
}
public function connect(){
$this->mysql = mysql_connect($this->host, $this->username,
$this->password) or
die("Could not connect to database. ". $this->username .
"@" . $this->host . " passwrod: YES!");
if (!mysql_select_db($this->db_name)) die("Cannot select database!");
}
}
$database = new MySqlDataBase($host,$username,$password,$db_name);
$database->connect();
答案 0 :(得分:1)
localhost
端口3306
,就像MySql Workbench一样。答案 1 :(得分:0)
您显然错误的凭据连接到数据库。 这就是为什么你得到错误“无法连接到MySQL”。 检查您发送的变量和用户名和密码,以便实现连接。
答案 2 :(得分:0)
尝试这样做,没有类声明:
// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);