我知道这个问题已被多次询问,但在我搜索过的所有其他帖子中,我都找不到答案。我似乎无法连接到数据库。
如果有帮助,我的操作系统是Ubuntu 12.04。
以下是config.php
的代码:
<?php
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','121212aa');
define('DB_NAME','PHP-Wizard');
?>
还有Database.php
<?php
class Database{
public $host = DB_HOST;
public $username = DB_USER;
public $password = DB_PASS;
public $db_name = DB_NAME;
public $link;
public $error;
/*
* Class Constructor
*/
public function __construct(){
//Call Connect Function
$this->connect();
}
/*
* Connector
*/
private function connect(){
$this->link = new mysqli($this->host, $this->username, $this->password, $this->db_name);
if(!$this->link){
$this->error = "Connection Failed: ".$this->link->connect_error;
return false;
}
}
/*
* Select
*/
public function select($query){
$result = $this->link->query($query) or die($this->link->error.__LINE__);
if($result->num_rows > 0){
return $result;
} else {
return false;
}
}
/*
* Insert
*/
public function insert($query){
$insert_row = $this->link->query($query) or die($this->link->error.__LINE__);
//Validate Insert
if($insert_row){
header("Location: index.php?msg=".urlencode('Record Added'));
exit();
} else {
die('Error : ('. $this->link->errno .') '. $this->link->error);
}
}
/*
* Update
*/
public function update($query){
$update_row = $this->link->query($query) or die($this->link->error.__LINE__);
//Validate Insert
if($update_row){
header("Location: index.php?msg=".urlencode('Record Updated'));
exit();
} else {
die('Error : ('. $this->link->errno .') '. $this->link->error);
}
}
/*
* Delete
*/
public function delete($query){
$delete_row = $this->link->query($query) or die($this->link->error.__LINE__);
//Validate Insert
if($delete_row){
header("Location: index.php?msg=".urlencode('Record Deleted'));
exit();
} else {
die('Error : ('. $this->link->errno .') '. $this->link->error);
}
}
以下是我尝试打开index.php
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in /opt/lampp/htdocs/Forum/Database.php on line 23
Warning: mysqli::query(): Couldn't fetch mysqli in /opt/lampp/htdocs/Forum/Database.php on line 35
Warning: Database::select(): Couldn't fetch mysqli in /opt/lampp/htdocs/Forum/Database.php on line 35
为什么我无法连接到数据库的任何答案?
答案 0 :(得分:2)
检查你的mysql凭证......
您可以通过运行服务器来重置root密码 --skip-grant-tables并以root用户身份(或使用sudo)运行以下命令而无需密码登录:
# service mysql stop
# mysqld_safe --skip-grant-tables &
$ mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("YOUR-NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit
# service mysql stop
# service mysql start
$ mysql -u root -p
答案 1 :(得分:1)
可能的原因是您的数据库凭据错误。如果您忘记了密码,可以轻松更改。正如你在其中一条评论中提到的那样(可以重新安装xampp修复它)。那么它不会反而尝试更改密码
访问
http://localhost/phpmyadmin
//Then visit
User > Select user and click edit privileges > Login Information
您可以在这里用新的密码覆盖旧密码
如果您在远程服务器(Cpanel)上,则可以从数据库选项重置密码
此外,我在您的代码中没有看到任何内容,包括config.php
,请确保其中包含您的类文件
答案 2 :(得分:0)
您可以通过转到控制台并输入以下内容来检查您的凭据是否正确
mysql -uroot -p121212aa
如果您收到相同的错误,则很可能是凭据。