I know this question has been asked many times, but in all other posts i searched, i just couldn't find the answer. I just cant seem to connect to the database.
My OS is Ubuntu 12.04, if it helps.
Here is my code for config.php
:
<?php
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','121212aa');
define('DB_NAME','PHP-Wizard');
?>
And also 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);
}
}
And here is the error i get when i try to open 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
Any answers as to why i cant connect to the Database?
check your mysql credential....
You can reset the root password by running the server with --skip-grant-tables and logging in without a password by running the following as root (or with 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
Possible reasons are your database credentials are wrong. If you don't remember your password you can easily change. As you mentioned in one of comments (could reinstalling xampp fix it). Well it won't Instead try changing password
Visit
http://localhost/phpmyadmin
//Then visit
User > Select user and click edit privileges > Login Information
Here you can overwrite old password with new one
If you are on remote server (Cpanel) you can reset your password from database options
Also i don't see anywhere in your code including config.php
make sure its including in you class file
You might be able to check if your credentials are correct by going to the console and typing:
mysql -uroot -p121212aa
If you are getting the same error it is likely to be credentials.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.